There is a problem with my code:
class X<T extends B> [...]
// this.p.a :: B | null
methodA(a: T):void {[...]}
methodB(): void {
if(this.p.a){ // :: B
this.methodA(this.p.a) // Error
My intention was for T
to be any type that extends B
, and then pass a property of type B
to it. However, Typescript is unable to infer that B
satisfies the type parameter T
in the last line of the code snippet above.
The error message received is:
error TS2345: Argument of type 'B' is not assignable to parameter of type 'T'.
Why is this happening?
Do I need to change the offending type declaration to B
instead of T
and rely on inheritance instead?