Enforce comparing typeof expressions against valid strings
Using the recommended config from @eslint/js in a configuration file enables this rule
Some problems reported by this rule are manually fixable by editor suggestions
For a vast majority of use cases, the result of the typeof operator is one of the following string literals: "undefined", "object", "boolean", "number", "string", "function", "symbol", and "bigint". It is usually a typing mistake to compare the result of a typeof operator to other string literals.
This rule enforces comparing typeof expressions to valid string literals.
Examples of incorrect code for this rule:
/*eslint valid-typeof: "error"*/
typeof foo ===
typeof foo ==
typeof bar !=
typeof bar !==
Examples of correct code for this rule:
/*eslint valid-typeof: "error"*/
typeof foo === "string"
typeof bar == "undefined"
typeof foo === baz
typeof bar === typeof qux
This rule has an object option:
"requireStringLiterals": true allows the comparison of typeof expressions with only string literals or other typeof expressions, and disallows comparisons to any other value. Default is false.Examples of incorrect code with the { "requireStringLiterals": true } option:
/*eslint valid-typeof: ["error", { "requireStringLiterals": true }]*/
typeof foo ===
typeof bar ==
typeof baz ===
typeof qux ===
typeof baz ===
typeof foo ==
Examples of correct code with the { "requireStringLiterals": true } option:
/*eslint valid-typeof: ["error", { "requireStringLiterals": true }]*/
typeof foo === "undefined"
typeof bar == "object"
typeof baz === "string"
typeof bar === typeof qux
You may want to turn this rule off if you will be using the typeof operator on host objects.
This rule was introduced in ESLint v0.5.0.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/valid-typeof