]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/valid-typeof.md
66f972475b634484491127fd9ce24870923182fe
[pve-eslint.git] / eslint / docs / src / rules / valid-typeof.md
1 ---
2 title: valid-typeof
3 layout: doc
4 rule_type: problem
5 further_reading:
6 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
7 ---
8
9
10
11
12
13 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.
14
15 ## Rule Details
16
17 This rule enforces comparing `typeof` expressions to valid string literals.
18
19 ## Options
20
21 This rule has an object option:
22
23 * `"requireStringLiterals": true` requires `typeof` expressions to only be compared to string literals or other `typeof` expressions, and disallows comparisons to any other value.
24
25 Examples of **incorrect** code for this rule:
26
27 ::: incorrect
28
29 ```js
30 /*eslint valid-typeof: "error"*/
31
32 typeof foo === "strnig"
33 typeof foo == "undefimed"
34 typeof bar != "nunber"
35 typeof bar !== "fucntion"
36 ```
37
38 :::
39
40 Examples of **correct** code for this rule:
41
42 ::: correct
43
44 ```js
45 /*eslint valid-typeof: "error"*/
46
47 typeof foo === "string"
48 typeof bar == "undefined"
49 typeof foo === baz
50 typeof bar === typeof qux
51 ```
52
53 :::
54
55 Examples of **incorrect** code with the `{ "requireStringLiterals": true }` option:
56
57 ::: incorrect
58
59 ```js
60 /*eslint valid-typeof: ["error", { "requireStringLiterals": true }]*/
61
62 typeof foo === undefined
63 typeof bar == Object
64 typeof baz === "strnig"
65 typeof qux === "some invalid type"
66 typeof baz === anotherVariable
67 typeof foo == 5
68 ```
69
70 :::
71
72 Examples of **correct** code with the `{ "requireStringLiterals": true }` option:
73
74 ::: correct
75
76 ```js
77 /*eslint valid-typeof: ["error", { "requireStringLiterals": true }]*/
78
79 typeof foo === "undefined"
80 typeof bar == "object"
81 typeof baz === "string"
82 typeof bar === typeof qux
83 ```
84
85 :::
86
87 ## When Not To Use It
88
89 You may want to turn this rule off if you will be using the `typeof` operator on host objects.