Skip to main content

Type GuardRuntime Type-Check to Narrow Type of Variable

aka Runtime-Type-Check, Runtime-Check

Type Guard used when Type too general, requires Narrow

typeof & instanceof are JS Operator that act as Type Guard

Equality Operator can be used as Type Guard to check for specific value

can only use === & !== to exclude Union Type if component is Singleton

Narrow

to reduce static type T to a (narrowed down) subset of T

Assertion Function

Type Predicate

Union Type can be too general & require Type Guard for Narrow

nullish parameters type can be too generaltsxfunction f(arg: string|null) {}
function f(arg: string|undefined) {}

References