typeofComparisons
Reports typeof expressions that compare impossible string literals.
✅ This rule is included in the ts untyped
preset.
The typeof
operator in JavaScript returns one of a specific set of string values: "bigint"
, "boolean"
, "function"
, "number"
, "object"
, "string"
, "symbol"
, or "undefined"
.
Comparing the result of typeof
to any other string literal is usually a typo and the comparison will never be true.
This rule enforces that typeof
expressions are only compared to valid string literals.
Examples
Section titled “Examples”if (typeof value === "strnig") { processString(value);}
if (typeof callback !== "fucntion") { throw new Error("Invalid callback");}
if (typeof data == "undefimed") { initialize();}
if (typeof count === "nunber") { calculate(count);}
if (typeof value === "string") { processString(value);}
if (typeof callback !== "function") { throw new Error("Invalid callback");}
if (typeof data == "undefined") { initialize();}
if (typeof count === "number") { calculate(count);}
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your project is a rare one that operates on non-standard objects, such as ancient versions of obscure browsers, this rule might not benefit you. For all other projects, using either this rule or a full type-checker should always be done.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useValidTypeof
- Deno:
valid-typeof
- ESLint:
valid-typeof
- Oxlint:
eslint/valid-typeof
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.