Skip to content

Commit ea6b6d9

Browse files
authoredMar 16, 2025··
docs(linter): Improve docs for eslint-valid-typeof (#9797)
Adds correctness examples and document options in line with #6050
1 parent 227d203 commit ea6b6d9

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed
 

‎crates/oxc_linter/src/rules/eslint/valid_typeof.rs

+46-10
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,60 @@ pub struct ValidTypeof {
3131
}
3232
declare_oxc_lint!(
3333
/// ### What it does
34-
/// Enforce comparing `typeof` expressions against valid strings
34+
///
35+
/// Enforce comparing `typeof` expressions against valid strings.
3536
///
3637
/// ### Why is this bad?
37-
/// It is usually a typing mistake to compare the result of a `typeof`
38-
/// operator to other string literals.
3938
///
40-
/// ### Example
39+
/// For a vast majority of use cases, the result of the `typeof` operator is one of the
40+
/// following string literals: `"undefined"`, `"object"`, `"boolean"`, `"number"`, `"string"`,
41+
/// `"function"`, `"symbol"`, and `"bigint"`. It is usually a typing mistake to compare the
42+
/// result of a `typeof` operator to other string literals.
43+
///
44+
/// ### Examples
45+
///
46+
/// Examples of **incorrect** code for this rule:
4147
/// ```js
42-
/// // requireStringLiterals: false
43-
/// // incorrect:
4448
/// typeof foo === "strnig"
45-
/// // correct:
49+
/// typeof foo == "undefimed"
50+
/// typeof bar != "nunber" // spellchecker:disable-line
51+
/// typeof bar !== "fucntion" // spellchecker:disable-line
52+
/// ```
53+
///
54+
/// Examples of **correct** code for this rule:
55+
/// ```js
4656
/// typeof foo === "string"
57+
/// typeof bar == "undefined"
4758
/// typeof foo === baz
59+
/// typeof bar === typeof qux
60+
/// ```js
4861
///
49-
/// // requireStringLiterals: true
50-
/// // incorrect:
51-
/// typeof foo === baz
62+
/// ### Options
63+
///
64+
/// #### requireStringLiterals
65+
///
66+
/// `{ type: boolean, default: false }`
67+
///
68+
/// The `requireStringLiterals` option when set to `true`, allows the comparison of `typeof`
69+
/// expressions with only string literals or other `typeof` expressions, and disallows
70+
/// comparisons to any other value. Default is `false`.
71+
///
72+
/// With `requireStringLiterals` set to `true` the following are examples of incorrect code:
73+
/// ```js
74+
/// typeof foo === undefined
75+
/// typeof bar == Object
76+
/// typeof baz === "strnig"
77+
/// typeof qux === "some invalid type"
78+
/// typeof baz === anotherVariable
79+
/// typeof foo == 5
80+
/// ```
81+
///
82+
/// With `requireStringLiterals` set to `true` the following are examples of correct code:
83+
/// ```js
84+
/// typeof foo === "undefined"
85+
/// typeof bar == "object"
86+
/// typeof baz === "string"
87+
/// typeof bar === typeof qux
5288
/// ```
5389
ValidTypeof,
5490
eslint,

0 commit comments

Comments
 (0)
Please sign in to comment.