-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Syntactic checks against comparing to null/undefined in strictNullChecks mode #60425
Comments
Duplicate of #29200. |
I mean.. the other cases are covered by #11920. |
My understanding is that a major concern about #11920 was breaking code intentionally checking for null/undefined against an API excluding null/undefined. & the discussion on the PR to fix that issue #59559 (comment) seems to be concluding that it's too breaking to land without adding an extra flag. I'd expect this change to be significantly less breaking specifically because it would be syntactic only. If the TS team is interested in rescoping #11920 to be about cases only syntactically known to be not null/undefined, or doesn't want to add a syntactic-only check, I understand, but I would like to get feedback specifically on the idea of a syntactic-only check so that it's not lost in the noise in #11920. |
To clarify, I'm not proposing to flag this code from #11920 (comment): declare const value: number;
if (value !== null) { // <-- somehow valid, expected to be invalid, since number doesn't have null as a possible value
}
if (value !== '') { // <-- invalid as expected
} because |
Other cases that could be covered, motivated by the nullish coalesce checks from #59217: if (((x) => x) !== undefined) {}
if ("" !== undefined) {}
if ({foo: 3} !== undefined) {} |
I think this is a decent idea now that we have a notion of "syntactically provable to not be |
π 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 justundefined
in code like this: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
The text was updated successfully, but these errors were encountered: