-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Should warn against invalid typeof expression #29200
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
Comments
I think fixing this case without breaking many common patterns would require special casing the check (with |
What common pattern compares the result of |
@fatcerberus TypeScript always allows comparing with
IMO comparing something with
This check already exists as lint rule.
|
EDIT: Thanks @ajafff, that’s a more detailed response than mine! The common patterns are not those including To fix this issue the options seem to be changing the comparable relation for all |
Oh, that makes sense. I guess I assumed |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I recently ran into a similar issue, where it would have been nice if 'x === undefined' erred when x's type doesn't include undefined. Can someone elaborate on:
|
I assume there are 2 main reasons:
|
it's definitely an error for |
ah please fix this, I shot on my foot again today |
@DanielRosenwasser you marked this as working as intended, but I wonder if it's worth reopening this and limiting the scope of the enhancement. We know that
I feel like just these syntactic checks would help immensely in ensuring correctness without causing any unexpected errors in code. For example I know it's a common mistake to forget the quotes and do typeof foo === undefined |
There is a major problem with index signatures outside of function fn(config: Record<string, number>) {
if (typeof config.foo === undefined) {}
const num = config.foo
if (typeof num === undefined) {}
} Both should work but TS has no way to track the origin of this |
TypeScript Version: 3.2.2
Search Terms: typeof undefined
Code
Expected behavior:
Should warn as what
typeof 42 === 42
warns.Actual behavior:
No warnings
Playground Link:
http://www.typescriptlang.org/play/index.html#src=%2F%2F%20This%20condition%20will%20always%20return%20'false'%20since%20the%20types%20'%22string%22%20%7C%20%22number%22%20%7C%20%22bigint%22%20%7C%20%22boolean%22%20%7C%20%22symbol%22%20%7C%20%22undefined%22%20%7C%20%22object%22%20%7C%20%22function%22'%20and%20'42'%20have%20no%20overlap.%0Atypeof%2042%20%3D%3D%3D%2042%0A%0Atypeof%2042%20%3D%3D%3D%20undefined%0A%0Atypeof%2042%20%3D%3D%3D%20null
Related Issues:
The text was updated successfully, but these errors were encountered: