Skip to content

Syntactic checks against comparing to null/undefined in strictNullChecks modeΒ #60425

Open
@lauraharker

Description

@lauraharker

πŸ” Search Terms

nullish, strictNullChecks, equals, undefined, null

βœ… Viability Checklist

⭐ Suggestion

When strictNullChecks are enabled, disallow == and === comparisons to null/undefined for expressions that are syntactically known to not be undefined.

I'd imagine the definition of syntactically not null/undefined expressions would be similar to that from #59217.

This is similar to #11920, but I think would be easier to land because it won't error on defensive programming null checks.

πŸ“ƒ Motivating Example

TypeScript's typeof operator always returns a string, but it can be hard to spot the difference between checking "undefined" and just undefined in code like this:

if (typeof foo === undefined) {

In strictNullChecks mode, TypeScript will catch unnecessary comparisons like this, for expressions that at runtime it knows can never be null or undefined.

πŸ’» Use Cases

  1. What do you want to use this for? catch user errors
  2. What shortcomings exist with current approaches? there's the https://typescript-eslint.io/rules/no-unnecessary-condition/, but that's type-based so does have the problem of flagging defensive programming. it's also nice to have things be in the compiler instead of a separate tool.
  3. What workarounds are you using in the meantime? nothing at the moment, if this is infeasible will probably create a custom lint check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experimentation NeededSomeone needs to try this out to see what happensSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions