Skip to content

TS allows typeof foo === undefined & typeof foo === null #29405

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

Closed
tommytroylin opened this issue Jan 14, 2019 · 3 comments
Closed

TS allows typeof foo === undefined & typeof foo === null #29405

tommytroylin opened this issue Jan 14, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@tommytroylin
Copy link

TypeScript Version: 3.2.2

Search Terms: typeof

Code

typeof window == null
typeof window == undefined

Expected behavior:
compiler error

This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and 'null' have no overlap.

Actual behavior:
no error

My opinion:

This behavior allows quick null/undefined checks regardless of type information.

but typeof foo === undefined needs to be considered as a typo much more.

@jack-williams
Copy link
Collaborator

Duplicate of #29200

@fatcerberus
Copy link

but typeof foo === undefined needs to be considered as a typo much more.

The compiler doesn't special-case typeof in an expression for the purpose of type checking. It looks the same to the compiler as, e.g., stringVar === undefined (which is deliberately allowed, as you've pointed out).

@weswigham weswigham added the Duplicate An existing issue was already created label Jan 14, 2019
@tommytroylin
Copy link
Author

thanks @jack-williams for references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants