Skip to content

Disallow implicit boolean casting of strings and numbers #49

Closed
@novemberborn

Description

@novemberborn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: new plugin ruleNew rule request for eslint-pluginhas prthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions