5

Conside the following simple interface and a class:

interface ITest{
    id :string;
}

class SuperClass<T extends ITest>{
    Start(){
        var item=<ITest>{};
        this.Do(item);
    }
    Do(item: T){
        alert(item);
    }

}

The line with this.Do(item) shows the error: Argument of type ITest is not assignable to type T. Why?

1 Answer 1

4
Do(item: T){
    alert(item);
}

The method Do expects a parameter of type T.

    var item=<ITest>{};

A variable item is created, of type ITest.

    this.Do(item);

T extends ITest, but ITest doesn't extend T. The variable item has the type ITest, not the type T.

This code compiles:

Start(){
    var item=<T>{};
    this.Do(item);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.