Should warn against invalid typeof expression
See original GitHub issueTypeScript Version: 3.2.2
Search Terms: typeof undefined
Code
// This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '42' have no overlap.
typeof 42 === 42
typeof 42 === undefined
typeof 42 === null
Expected behavior:
typeof 42 === undefined
typeof 42 === null
Should warn as what typeof 42 === 42
warns.
Actual behavior:
No warnings
Related Issues:
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Invalid typeof value '{a}' - JSLint Error Explanations
The "Invalid typeof value '{a}'" error is thrown when JSHint encounters a comparison with a typeof expression on one side and an invalid...
Read more >why is "typeof v === 'string'" considered invalid by eslint
It's because you're using Angular, which has a built-in isString method, and you have the Angular plugin installed for eslint.
Read more >enforce comparing typeof expressions against valid strings (valid ...
This rule enforces comparing typeof expressions to valid string literals. Options. This rule has an object option: "requireStringLiterals": true requires typeof ...
Read more >C# Compiler warning waves | Microsoft Learn
Warning wave 5. The is and as expressions always return false for a static type because you can't create instances of a static...
Read more >Documentation - Narrowing - TypeScript
Argument of type 'string | number' is not assignable to parameter of type 'number' ... In JavaScript, we can use any expression in...
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 FreeTop 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
Top GitHub Comments
@fatcerberus TypeScript always allows comparing with
null
orundefined
(because reasons). There’s nothing special about comparing the result of atypeof
expression. That’s what @jack-williams means by:IMO comparing something with
null
orundefined
shouldn’t be treated different from any other value. If you need to, your type declarations are probably wrong. But that’s already discussed in another issue and was declined for compatibility reasons. Therefore I’ve written a lint rule, see below.This check already exists as lint rule.
strict-type-predicates
unknown
,bigint
or type parameters)no-useless-predicate
ah please fix this, I shot on my foot again today