Skip to content

Disallow implicit boolean casting of strings and numbers #49

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

Closed
novemberborn opened this issue Jan 26, 2018 · 6 comments
Closed

Disallow implicit boolean casting of strings and numbers #49

novemberborn opened this issue Jan 26, 2018 · 6 comments
Labels
enhancement: new plugin rule New rule request for eslint-plugin has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@novemberborn
Copy link

It'd be great to have a lint rule that disallows (possible) strings and numbers from being implicitly converted to booleans:

const str: string | null = ''
if (str) {
  // ignores the empty string, the programmer likely attempted to guard against null
}

const num: number | undefined = 0
if (!num) {
  // ignores the 0 value, but the programmer likely attempted to guard against undefined
}

Besides if statements this would apply to ternary statements as well as &&, || and ! operators.

For the above examples passing code could be:

const str: string | null = ''
if (typeof str === 'string') {
  // …
}
if (str !== null) {
  // …
}

const num: number | undefined = 0
if (typeof num !== 'undefined') {
  // ...
}

I can see a stricter version of this rule which enforces explicit conversions even if a value can only be a string or number.

@j-f1
Copy link
Contributor

j-f1 commented Jan 26, 2018

We’re interested in doing this in GitHub Desktop: desktop/desktop#3538. I tried to add a rule in desktop/desktop#3550, but the type information available was limited, and @iAmWillShepherd suggested that we try to write this rule with TSLint instead.

@novemberborn
Copy link
Author

the type information available was limited

I suppose you'd need to use the Language Service API to access accurate type information, especially when you're accessing properties that were defined in a separate file. Not sure how easy it'd be to integrate that into ESLint's lifecycle.

@Coder-256
Copy link

Bump! I would love to see this too. This could be added as an option for the no-implicit-coercion rule's boolean property, because if (x) does an implicit boolean conversion, just the same as !!x.

@bradzacher
Copy link
Member

flow supports this via their sketchy-null rules.
https://flow.org/en/docs/linting/rule-reference/#toc-sketchy-null

Speaking from experience over my last few months at facebook, it's actually really, really annoying; most engineers I've spoken to hate it!

But if there is demand from the community, it's a rule we could look into.

@JamesHenry JamesHenry transferred this issue from bradzacher/eslint-plugin-typescript Jan 18, 2019
@bradzacher bradzacher added enhancement New feature or request package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin enhancement: new plugin rule New rule request for eslint-plugin labels Jan 18, 2019
@SpainTrain
Copy link

As a temporary workaround, one can use @typescript-eslint/eslint-plugin-tslint with tslint rule strict-boolean-expressions (https://github.com/palantir/tslint/blob/master/src/rules/strictBooleanExpressionsRule.ts).

I miss this feature from Flow and would love to see this as a rule. Chatter I saw on the TypeScript tracker indicated it is unlikely to be added to the compiler.

@novemberborn
Copy link
Author

strict-boolean-expressions is now available: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/strict-boolean-expressions.md

@bradzacher bradzacher added has pr there is a PR raised to close this and removed enhancement New feature or request labels Jul 12, 2019
@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

5 participants