type guard

A run time check, such as typeof x === "string" or a custom function, that the compiler uses to narrow a type.

In more detail

Built-in guards include typeof, instanceof and in. A custom type guard is a function returning a type predicate such as val is Record<string, unknown>, which packages a check for reuse.

Related terms

Taught in

References