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
false positive error after null checking with typed boolean variable instead non typed variable #56369
The following code doesn't compile. It can't recognize the null checking of the variable
index.ts
// ...constvariable=test();// Can be literally any type, any, boolean, BooleanconstisVariableNull: any=!variable;if(!isVariableNull){// Property 'field' does not exist on type 'Example | null'.(2339)const{ field }=variable;}
Equivalent working code
The following code is equivalent to the previous one and correctly compiles.
index.ts
// ...constvariable=test();// Mustn't be typedconstisVariableNull=!variable;if(!isVariableNull){// Property 'field' can be normally used hereconst{ field }=variable;}
🙁 Actual behavior
TypeScript can't recognize null checking of variables when the value is assigned to a typed variable and later used to do the null checking
🙂 Expected behavior
TypeScript should recognize the null checking and compile the code normally (Like it's equivalent snippet)
You forgot to fill out the issue template (missing playground link).
This is working as intended. Control flow analysis of aliases expressions does not work when your variable has a type annotation, as per #44730:
Narrowing through indirect references occurs only when the conditional expression or discriminant property access is declared in a const variable declaration with no type annotation, [...]
Activity
MartinJohns commentedon Nov 12, 2023
You forgot to fill out the issue template (missing playground link).
This is working as intended. Control flow analysis of aliases expressions does not work when your variable has a type annotation, as per #44730:
typescript-bot commentedon Nov 16, 2023
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.