Skip to content

No Runtime Typeof

Error
small-rules/no-runtime-typeof

Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary.

Rationale

Ported from dmmulroy/anti-slop. A typeof check narrows a representation but does not establish the value’s full contract. Parse at the I/O boundary instead. Set allowInTypeGuards only for schema-free codebases that deliberately use type-predicate helpers.

Diagnostic Messages

runtimeTypeof
A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value.

Configuration

This rule accepts one options object after the severity.

allowInTypeGuardsOptional

Allow `typeof` inside functions whose return type is a type predicate (`value is T`) or an assertion predicate (`asserts value is T`).

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-runtime-typeof": [
"error",
{
"allowInTypeGuards": false
}
]
}
}

Examples

ad hoc typeof narrowing
if (typeof input === 'string') use(input);
explicit type-guard exception
function isString(value: unknown): value is string { return typeof value === "string"; }