question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enforce comparing `typeof` expressions against string literals

See original GitHub issue

I 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'

http://eslint.org/docs/rules/valid-typeof

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
ferosscommented, Feb 9, 2017

This will be part of standard v9. Only one repo was impacted and it was doing this:

const OBJ = 'object'

module.exports = function merge (a, b, checked) {
  if (checked || typeof a === OBJ) {

Can’t see a reason that’s preferable to typeof a === 'object', so not too worried about this case.

1reaction
dcousenscommented, Sep 19, 2016

@feross I feel like we don’t thank you enough. Thanks so much for the work, totally understandable. Looking forward to v9.0 😄 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found