codelegant
codelegant

Reputation: 580

When use Class as a Type,can't use the static method of it

There are two Class:

class Observer {
    static Update(value) { }
}
class ObserverList {
    private observerList: Observer[];
    constructor() {
        this.observerList = [];
    }
    Get(index: number): Observer {
        if (index > -1 && index < this.observerList.length) {
            return this.observerList[index];
        }
    }
}

I use then like this:

var obList=new ObserverList();
obList.Get(3).Update();

And then waring me Property 'Update' does not exist on Type Observer。 Is this a wrong way to use Class Observer as a return type?

enter image description here

Upvotes: 0

Views: 55

Answers (1)

bigboy
bigboy

Reputation: 96

You should not use Observer.Update method as static.

Also in typescript, its good practice to mark observers method with type signature, like in following snippet:

class Observer {
    public Update(value: <yourParameterType>): void { }
}

If you have this Observer class, you can call Update method on it

obList.Get(3).Update();

Upvotes: 1

Related Questions