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
typeT={a:any}|{b:any};letmyObject:{c:T};functionhasA(value:T):value is {a:any}{return'a'invalue;}if(hasA(myObject.c)){myObject.c.a;// <- the error goes from here}
Expected behavior:
No error.
Actual behavior:
Error:(24, 14) TS2339: Property 'a' does not exist on type '{ a: any; } | { b: any; }'.
Shouldn't it be considered as a bug?
The text was updated successfully, but these errors were encountered:
That is, the problem is that type guards don't yet work with property accesses (like myObject.c). Your example works if the property access is done before the type guard, which can then use a simple variable:
letc=myObject.c;if(hasA(c)){c.a;// <- no error here anymore}
Just read about this new TypeScript feature and tried to use it in my project, but...
TypeScript Version:
1.8.9
Code
Expected behavior:
No error.
Actual behavior:
Error:(24, 14) TS2339: Property 'a' does not exist on type '{ a: any; } | { b: any; }'.
Shouldn't it be considered as a bug?
The text was updated successfully, but these errors were encountered: