1 # enforce comparing `typeof` expressions against valid strings (valid-typeof)
3 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.
7 This rule enforces comparing `typeof` expressions to valid string literals.
11 This rule has an object option:
13 * `"requireStringLiterals": true` requires `typeof` expressions to only be compared to string literals or other `typeof` expressions, and disallows comparisons to any other value.
15 Examples of **incorrect** code for this rule:
18 /*eslint valid-typeof: "error"*/
20 typeof foo === "strnig"
21 typeof foo == "undefimed"
22 typeof bar != "nunber"
23 typeof bar !== "fucntion"
26 Examples of **correct** code for this rule:
29 /*eslint valid-typeof: "error"*/
31 typeof foo === "string"
32 typeof bar == "undefined"
34 typeof bar === typeof qux
37 Examples of **incorrect** code with the `{ "requireStringLiterals": true }` option:
40 typeof foo === undefined
42 typeof baz === "strnig"
43 typeof qux === "some invalid type"
44 typeof baz === anotherVariable
48 Examples of **correct** code with the `{ "requireStringLiterals": true }` option:
51 typeof foo === "undefined"
52 typeof bar == "object"
53 typeof baz === "string"
54 typeof bar === typeof qux
59 You may want to turn this rule off if you will be using the `typeof` operator on host objects.
63 * [MDN: `typeof` documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)