You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Narrowing does not work for variables typed with type parameters #6445
interfaceBase{b}interfaceDerivedextendsBase{d}declarefunctionisDerived(x: Base): x is Derived;functionf<TextendsBase>(x: T,y: T){if(isDerived(x)&&isDerived(y)){returnx.d===y.d;}returnfalse;}
Currently, this errors because both x and y still have type T, so they are missing the d property.
getNarrowedType doesn't check whether the type it's narrowing is a type parameter with a constraint. It just calls isTypeAssignableTo(Derived, T), which is false.
getNarrowedType needs to instead check isTypeAssignableTo(Derived, Base), but I'm not sure whether it's appropriate to narrow to Derived at that point, or to T extends Derived. Probably the latter.
Activity
sandersn commentedon Mar 7, 2016
getNarrowedTypedoesn't check whether the type it's narrowing is a type parameter with a constraint. It just callsisTypeAssignableTo(Derived, T), which is false.getNarrowedTypeneeds to instead checkisTypeAssignableTo(Derived, Base), but I'm not sure whether it's appropriate to narrow toDerivedat that point, or toT extends Derived. Probably the latter.Wesley Wigham (@weswigham), do you have an idea of the right thing to do here?
mhegazy commentedon Apr 21, 2016
this is handled by #8010
ahejlsberg commentedon Apr 22, 2016
Fixed in #8010.