type narrowing

The compiler refining a broad type to a more specific one inside a block, after a check proves which case applies.

In more detail

Inside if (typeof x === "string"), x is a string. Narrowing comes from typeof, instanceof, the in operator, equality and truthiness checks, discriminant tags, type predicates and assertion functions. It is how you safely work with unions and unknown.

Related terms

Taught in

References