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"
and "symbol"
. It is usually a typing mistake to compare the result of a typeof
operator to other string literals.
This rule enforces comparing typeof
expressions to valid string literals.
Examples of incorrect code for this rule:
/*eslint valid-typeof: "error"*/
typeof foo === "strnig"
typeof foo == "undefimed"
typeof bar != "nunber"
typeof bar !== "fucntion"
Examples of correct code for this rule:
/*eslint valid-typeof: "error"*/
typeof foo === "string"
typeof bar == "undefined"
typeof foo === baz
typeof bar === typeof qux
You may want to turn this rule off if you will be using the typeof
operator on host objects.