Enforce comparing `typeof` expressions against string literals
See original GitHub issueI propose adding { "requireStringLiterals": true }
to the valid-typeof
rule that we already enforce.
This requires typeof
expressions to only be compared to string literals, and disallows comparisons to any other value.
Examples of incorrect code:
typeof foo === undefined
typeof bar == Object
typeof baz === 'strnig'
typeof qux === 'some invalid type'
typeof baz === anotherVariable
typeof foo == 5
typeof bar === typeof qux
Examples of correct code:
typeof foo === 'undefined'
typeof bar == 'object'
typeof baz === 'string'
Issue Analytics
- State:
- Created 7 years ago
- Reactions:11
- Comments:8 (8 by maintainers)
Top Results From Across the Web
valid-typeof - ESLint - Pluggable JavaScript Linter
This rule enforces comparing typeof expressions to valid string literals. Options. This rule has an object option: "requireStringLiterals": true requires typeof ...
Read more >Valid-typeof - ESLint - W3cubDocs
This rule enforces comparing typeof expressions to valid string literals. Options. This rule has an object option: "requireStringLiterals": true requires typeof ...
Read more >valid-typeof - ESLint Config
Requires typeof expressions to only be compared to string literals or other typeof expressions, and disallows comparisons to any other value.
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 >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 >
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
This will be part of standard v9. Only one repo was impacted and it was doing this:
Can’t see a reason that’s preferable to
typeof a === 'object'
, so not too worried about this case.@feross I feel like we don’t thank you enough. Thanks so much for the work, totally understandable. Looking forward to
v9.0
😄 👍