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
classA<T>{private_: T;}classB<T>{private_: T;}functionf(){varo: A<void>|B<void>=newB<void>();if(!(oinstanceofB))returno;// B<void>, should be A<void>o;// B<any>, should be B<boid>}
Expected behavior:
A type of o narrowed by type guard inside if statement is A<void>.
A type of o narrowed by type guard after if statement is B<void>.
Actual behavior:
A type of o narrowed by type guard inside if statement is A<void>.
A type of o narrowed by type guard after if statement is B<any>.
The text was updated successfully, but these errors were encountered:
I think the 'problem' here is that you assigned a new B<void> instance in the declaration of o. TypeScript has accordingly narrowed o to B<void> for the rest of the function body.
To see the behaviour you are expecting, you can add an <any> cast to prevent the initial narrowing:
classA<T>{private_: T;}classB<T>{private_: T;}functionf(){varo: A<void>|B<void>=<any>newB<void>();// <=====if(!(oinstanceofB))returno;// A<void>, should be A<void>o;// B<void>, should be B<boid>}}
TypeScript Version: 2.0.0
Code
Expected behavior:
A type of
o
narrowed by type guard inside if statement isA<void>
.A type of
o
narrowed by type guard after if statement isB<void>
.Actual behavior:
A type of
o
narrowed by type guard inside if statement isA<void>
.A type of
o
narrowed by type guard after if statement isB<any>
.The text was updated successfully, but these errors were encountered: