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
Currently type guards are limited only to bodies of conditionals.
A simple example with 'if':
functionf(s: string|number){if(typeofs==="string"){// s is 'string'}else{// s is 'number'}}
But what if I write the same code, but another way:
functionf(s: string|number){if(typeofs==="string"){// s is 'string'// do somethingreturn;}// s is 'string | number', according to the compiler// but control flow of 'if' block flows directly to a function exit// that's why, s is actually 'number' here, if no assignments are made}
So, the suggestion is to augment the rule for 'if' as follows:
If the false branch is not present, and all control flow exits of the true branch point exactly to function exits, the type of a variable or parameter is narrowed by a type guard in the code after 'if' statement when false, provided the code after 'if' statement contains no assignments to the variable or parameter.
If all control flow exists of the false branch point exactly to function exits, the type of a variable or parameter is narrowed by a type guard in the code after 'if' statement when true, provided the code after 'if' statement contains no assignments to the variable or parameter.
The text was updated successfully, but these errors were encountered:
Currently type guards are limited only to bodies of conditionals.
A simple example with 'if':
But what if I write the same code, but another way:
So, the suggestion is to augment the rule for 'if' as follows:
The text was updated successfully, but these errors were encountered: