Skip to content

typeofComparisons

Reports typeof expressions that compare impossible string literals.

✅ This rule is included in the ts untyped presets.

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.

if (typeof
const value: any
value
=== "strnig") {
Error ts(2367) ― This comparison appears to be unintentional because the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"strnig"' have no overlap.
const processString: any
processString
(
const value: object
value
);
}
if (typeof
const callback: any
callback
!== "fucntion") {
Error ts(2367) ― This comparison appears to be unintentional because the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"fucntion"' have no overlap.
throw new
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
("Invalid callback");
}
if (typeof
const data: any
data
== "undefimed") {
Error ts(2367) ― This comparison appears to be unintentional because the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"undefimed"' have no overlap.
const initialize: any
initialize
();
}
if (typeof
const count: any
count
=== "nunber") {
Error ts(2367) ― This comparison appears to be unintentional because the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"nunber"' have no overlap.
const calculate: any
calculate
(
const count: object
count
);
}

This rule is not configurable.

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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.