Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Add compile flag: StrictNumberBooleanChecks #21993

Closed
bertyhell opened this issue Feb 16, 2018 · 1 comment
Closed

Suggestion: Add compile flag: StrictNumberBooleanChecks #21993

bertyhell opened this issue Feb 16, 2018 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@bertyhell
Copy link

bertyhell commented Feb 16, 2018

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:

let myAttribute: number | undefined = undefined;
let value: number = myAttribute || 50;
console.log('value: ' + value);

But the value could also be 0. In which case the value will default to 50, which possibly isn't what the user intended.

let myAttribute: number | undefined = 0;
let value: number = myAttribute || 50;
console.log('value: ' + value); // value === 50, but user expected value === 0

Behavior when StrictNumberBooleanChecks=true

let myAttribute: number | undefined = 34;
let value: 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:

let myAttribute: number | undefined = 34;
let value: number = typeof myAttribute === 'undefined' ? 50 : myAttribute;
console.log('value: ' + value);

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

@mhegazy
Copy link
Contributor

mhegazy commented Feb 17, 2018

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.

duplicate of #8178 and #7306

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 17, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants