Skip to content

valid-typeof

Reports a typeof comparison whose other side is a string that typeof never returns (a typo such as 'strnig', or 'null'), or the identifier undefined instead of the string 'undefined'. A string template, a variable and a valid type name are not reported.

typeof returns one of "undefined", "object", "boolean", "number", "bigint", "string", "symbol" or "function". A comparison with any other string is always false, and a typo then silently turns a type check into dead code. typeof a === undefined compares a string with the value undefined, which is never true either (and typeof null is "object", not "null").

To fix, use one of the eight names, as a string.

Reported
...
typeof a === "strnig"; typeof a == "object"; "number" !== typeof a; typeof a === undefined; typeof a === typeof b;
...
js:
lint:
rules:
valid-typeof: error # off, warn, error (default)

Enabled by the presets: js/eslint-recommended, js/biome-recommended, js/oxlint.

Suppress one line with sudsy-disable-next-line valid-typeof in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
eslint valid-typeof partial the requireStringLiterals option is not supported