]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/valid-typeof.md
first commit
[pve-eslint.git] / eslint / docs / rules / valid-typeof.md
1 # enforce comparing `typeof` expressions against valid strings (valid-typeof)
2
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.
4
5 ## Rule Details
6
7 This rule enforces comparing `typeof` expressions to valid string literals.
8
9 ## Options
10
11 This rule has an object option:
12
13 * `"requireStringLiterals": true` requires `typeof` expressions to only be compared to string literals or other `typeof` expressions, and disallows comparisons to any other value.
14
15 Examples of **incorrect** code for this rule:
16
17 ```js
18 /*eslint valid-typeof: "error"*/
19
20 typeof foo === "strnig"
21 typeof foo == "undefimed"
22 typeof bar != "nunber"
23 typeof bar !== "fucntion"
24 ```
25
26 Examples of **correct** code for this rule:
27
28 ```js
29 /*eslint valid-typeof: "error"*/
30
31 typeof foo === "string"
32 typeof bar == "undefined"
33 typeof foo === baz
34 typeof bar === typeof qux
35 ```
36
37 Examples of **incorrect** code with the `{ "requireStringLiterals": true }` option:
38
39 ```js
40 typeof foo === undefined
41 typeof bar == Object
42 typeof baz === "strnig"
43 typeof qux === "some invalid type"
44 typeof baz === anotherVariable
45 typeof foo == 5
46 ```
47
48 Examples of **correct** code with the `{ "requireStringLiterals": true }` option:
49
50 ```js
51 typeof foo === "undefined"
52 typeof bar == "object"
53 typeof baz === "string"
54 typeof bar === typeof qux
55 ```
56
57 ## When Not To Use It
58
59 You may want to turn this rule off if you will be using the `typeof` operator on host objects.
60
61 ## Further Reading
62
63 * [MDN: `typeof` documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)