disallow comparing to null and undefined unless they are valid cases in strict null mode
See original GitHub issuethe rationale is the same as for --noUnusedLocals
which in this case would be --noUselessNullChecks
(i am not proposing a new flag, the existing --strictNullChecks
flag should be used, this is for illustration only)
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
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:29
- Comments:12 (3 by maintainers)
Top Results From Across the Web
strictNullChecks - TSConfig Option - TypeScript
When strictNullChecks is false , null and undefined are effectively ignored by the language. This can lead to unexpected errors at runtime.
Read more >Typescript strictNullChecks with limited scope - Stack Overflow
When you run tsc from inside directory a , you'll get strict null checks for a/code.ts . This approach isn't without drawbacks: If...
Read more >Strict null checking the Visual Studio Code codebase
To do this, we created a new TypeScript project file called tsconfig.strictNullChecks.json that enabled strict null checking and initially ...
Read more >How to enforce strict null checks in TypeScript - GeeksforGeeks
In Typescript to enforce strict null checks in tsconfig.json file, we need to enable “strictNullChecks” to true. When “strictNullChecks” is ...
Read more >Null vs. Undefined - TypeScript Deep Dive - Gitbook
Remember how I said you should use == null ? Of course you do (cause I just said it ^). Don't use it...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The rationale behind the current behavior is understandable. However, in my view it lends itself to sloppy typing. My strategy is to guard the boundaries where data comes in from unsafe (=non-TS) sources. And there,
null
andundefined
need to be accounted for, but that is really no special case as far as typing is concerned. Rather,null
andundefined
are just necessarily possible value types. Based on that, strict typing makes a lot of sense.Until today, I fully expected the behavior this issue calls for to be present when
strict
is active. Then I came across a case similar to the OP sample and wondered why there was no error message. I even checked the tsconfig to see if somebody had disabledstrict
orstrictNullChecks
.Please add this behavior, possibly behind a new setting 🙂
@DanielRosenwasser
@RyanCavanaugh This issue is marked
Awaiting More Feedback
. Can you say roughly what this issue would need to get some traction, how far off it is?i would suggest to make type safety the default, and have interop require explicit casting.