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
But the value could also be 0. In which case the value will default to 50, which possibly isn't what the user intended.
letmyAttribute: number|undefined=0;letvalue: number=myAttribute||50;console.log('value: '+value);// value === 50, but user expected value === 0
Behavior when StrictNumberBooleanChecks=true
letmyAttribute: number|undefined=34;letvalue: number=myAttribute||50;// Compiler warning/error: number variable could be zero, which would evaluate to false. Possible unintended behavior?console.log('value: '+value);
To avoid this compiler warning while the flag is true:
falsy values is a feature of JS, not sure why i would consider these "errors". The compiler correctly identify falsy values under --strictNullChecks. Seems to me that this can be a tslint rule instead.
The compiler flag: StrictNumberBooleanChecks would show a warning (or error) when the user writes an boolean evaluation of a number variable.
Code
User writes the code with a possible undefined value in mind:
But the value could also be 0. In which case the value will default to 50, which possibly isn't what the user intended.
Behavior when StrictNumberBooleanChecks=true
To avoid this compiler warning while the flag is true:
Playground Link:
https://www.typescriptlang.org/play/#src=let%20myAttribute%20%3D%20undefined%3B%0D%0Alet%20value%20%3D%20myAttribute%20%7C%7C%2050%3B%0D%0Aconsole.log('value%3A%20'%20%2B%20value)%3B%0D%0A%0D%0AmyAttribute%20%3D%2034%3B%0D%0Avalue%20%3D%20myAttribute%20%7C%7C%2050%3B%0D%0Aconsole.log('value%3A%20'%20%2B%20value)%3B%0D%0A%0D%0AmyAttribute%20%3D%200%3B%0D%0Avalue%20%3D%20myAttribute%20%7C%7C%2050%3B%0D%0Aconsole.log('value%3A%20'%20%2B%20value%20%2B%20'%20%2F%2F%20user%20expected%200%20as%20output')%3B
Search Terms:
zero checks boolean value strict avoid evaluate number int
The text was updated successfully, but these errors were encountered: