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
classC1{foo: number;}classC2{foo: number;bar: number;}functionf_not_C2(a: C1|C2){if(!(ainstanceofC2)){a.foo;// a has type C1, so only foo is available}}functionf_not_C1(a: C1|C2){if(!(ainstanceofC1)){a;// a should have type C2, but has type never!}}
Expected behavior:
The variable "a" in f_not_C1(...) should have type C2. Actual behavior:
The variable "a" in f_not_C1(...) has type never, making all properties unavailable.
The text was updated successfully, but these errors were encountered:
Duplicate of #10934 and many others. TypeScript's lack of nominal typing (#202) causes instanceof to behave oddly. Workarounds include adding some optional property to the classes to help TypeScript distinguish them:
classC1{__brand?: 'C1'foo: number;}classC2{__brand?: 'C2'foo: number;bar: number;}functionf_not_C1(a: C1|C2){if(!(ainstanceofC1)){a;// a is C2 now}}
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: TS 2.4
Code
Expected behavior:
The variable "a" in f_not_C1(...) should have type C2.
Actual behavior:
The variable "a" in f_not_C1(...) has type never, making all properties unavailable.
The text was updated successfully, but these errors were encountered: