microsoft / microsoft/TypeScript

Syntactic checks against comparing to null/undefined in strictNullChecks mode

Open
#60,425 7 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experimentation Needed Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 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 https://github.com/microsoft/TypeScript/pull/59217.

This is similar to https://github.com/microsoft/TypeScript/issues/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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the proposed behavior alongside issue #11920 and pull request #59217, which define related approaches to syntactically known non-nullish expressions. Done means strictNullChecks reports the requested unnecessary null/undefined comparisons without changing emitted JavaScript or runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.