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
classbox<T>{constructor(publicvalue: T){}check(): this is box<string>{returntypeofthis.value=='string';}assert(): asserts this is box<string>{if(typeofthis.value!='string')thrownewError();}privatetest(){this.assert();// type correctly narrowedthis.value.substring(0);}}functionmake() : box<string>|box<number>{returnnewbox('a');}functionassert(b: box<string>|box<number>): asserts b is box<string>{if(typeofb.value!='string')thrownewError();}constb=make();if(b.check()){// type correctly narrowedb.value.substring(0);}b.assert();// type not narrowed (substring does not exist on type 'string | number')b.value.substring(0);assert(b);// type correctly narrowedb.value.substring(0);
π Actual behavior
Type is not narrowed after method call
π Expected behavior
I expected the type to be narrowed after method call since it is narrowed within other methods or when using a function external to the type.
@RyanCavanaugh could you clarify what is the bug here? should it work or should it error in a visible way like @jcalz is suggesting? I think the latter but it would be great to have confirmation
π Search Terms
narrowing, assertion function
π Version & Regression Information
β― Playground Link
https://tsplay.dev/Wv3DYw
π» Code
π Actual behavior
Type is not narrowed after method call
π Expected behavior
I expected the type to be narrowed after method call since it is narrowed within other methods or when using a function external to the type.
Additional information about the issue
https://stackoverflow.com/questions/78879014/typescript-type-assertion-does-not-narrow-class-instance-from-union
The text was updated successfully, but these errors were encountered: