Unsound Type Guard Check
ID |
javascript.unsound_type_guard |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
dead-code, reliability, type-check |
Description
Reports a typeof comparison whose string operand is not one of the values typeof can produce:
if (typeof value === "array") { ... } // always false
if (typeof value !== "null") { ... } // always true
if (typeof value === "Array") { ... } // case-sensitive — wrong
The eight values typeof can return are:
"undefined", "object", "boolean", "number", "bigint", "string", "symbol", "function".
Anything else is statically decidable: an === comparison is always false and the inverse !== is always true.
Rationale
Unsound typeof checks are almost always typos:
-
"array"instead ofArray.isArray(value) -
"null"instead ofvalue === null -
"NaN"or"Number"instead of the correct lowercase result -
"undefined "(with a trailing space) instead of"undefined"
Each of these silently makes the guarded code unreachable (or reachable in every case), which masks the very kind of defensive check the developer intended.
Remediation
Use the correct comparison for the type you want to detect:
if (Array.isArray(value)) { ... }
if (value === null) { ... }
if (Number.isNaN(value)) { ... }
if (typeof value === "undefined") { ... } // fixed casing/spacing
Notes
The rule fires only when the string operand is a literal that is not in the eight-value set. Comparing two typeof expressions (typeof x === typeof y) is well-defined and is not reported.