-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
disallow comparing to null and undefined unless they are valid cases in strict null mode #11920
Comments
We intentionally allow this for the sake of defensive programming (i.e. defending against missing inputs from non-TS code). If there's enough demand we could add a flag or something. |
i would suggest to make type safety the default, and have interop require explicit casting. |
Internal implementation detail: right now we use the comparability relationship for type assertions (casting) - if we did this, we'd need to have a separate type relationship (or ad-hoc check) for allowing you to cast |
This seems to work good enough for me right now, but in the long run I would like to see this baked into the language. |
I would also like to have it implemented for the same reason as #14764: vulnerable refactoring. This proposal is a bit more broad, then #14764. Maybe they should be merged? In the recent refactoring I've changed
This silently stopped working and took quite a bit of effort to debug. It was also surprising that this didn't fail type checking. Maybe do it as |
I just encountered a bug similar to @devoto13. I was making some code work with |
We had bugs in our codebase due to this: For example: This is a bug: if (typeof foo === undefined) { // this is impossible
} it should be: if (typeof foo === 'undefined') {
} I know it is a dumb bug but shit happens and this is hard to notice, even with code reviews. The typesystem shouldn't allow this |
i'd like to vote for a flag or something for this; just refactored a few things from |
You can use the |
Has opinion changed on this at all? I see that in 2016 it was of the opinion that allowing this behavior would be better for defensive programming when dealing with non-TS code. Strangely enough, this behavior exists on the typeof null === null; The snippet above is likely something a beginner might do if after they attempt to check it against // @errors: 2367
typeof null === "null"; |
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, Until today, I fully expected the behavior this issue calls for to be present when Please add this behavior, possibly behind a new setting 🙂
@RyanCavanaugh |
👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript. ❌ Failed: -
Historical Information
|
Since this issue is "awaiting more feedback" I'd just like to add my feedback that allowing comparison to null for non-nullable types in strict mode is far from intuitive behavior. I've encountered bugs that would've been prevented had the compiler caught these type of impossible comparisons to null. I would love to see an additional flag for this if adding this behavior to |
Just had an infinite loop that lead me to this issue. I was surprised to find out that I can compare a string to null. const tokenStream = new Tokenizer(input);
while (tokenStream.peek() !== null) { // will never happen! Returns "" instead of null at EOF.
processNextToken(tokenStream.next());
} The problem is I was assuming that tokenStream.peek would return null at the end of the file, but it instead returns an empty string. The return type of the method is a non-nullable string. I really feel like the type system should have caught this before bricking my computer and making me hard reset. |
Also adding that I ran into a case where I was allowed to compare a
|
We also ran into a problem with if (typeof foo === undefined) { // this is impossible
} recently - would there be any way to disallow special case expressions that are definitely, syntactically, not |
the 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)The text was updated successfully, but these errors were encountered: