liveBook · Manning

Type guard

A type guard is a runtime-checkable condition that enables TypeScript to narrow an expression to a more specific type within a code path.

How 2 Manning books explain it

Essential TypeScript 5, Third Edition

A type guard is a runtime-checkable condition that allows the TypeScript compiler to narrow an expression to a more specific type within a code path.

How it is used
Narrows union values along checked code paths so operations specific to one possible type can be used safely.
How deep it goes
Works through primitive, nullable, and object checks, reusable predicates, and exhaustive narrowing; does not detail compiler internals.
Examples
Uses tax results, tuple elements, object arrays, and a decorator-added method to demonstrate checks in application code.
What you can do after
Apply runtime checks and predicate functions to narrow unions, handle null, and access type-specific properties or methods.

Read how Essential TypeScript 5, Third Edition explains type guard →

Acing the Frontend Interview

A type guard is a runtime check or predicate that narrows a value to a more specific TypeScript type.

How it is used
Uses runtime checks to narrow uncertain values and preserve TypeScript’s type safety.
How deep it goes
Explains narrowing through typeof and contrasts unknown with any, but does not develop custom guard predicates.
Examples
Demonstrates typeof narrowing on an unknown number and contrasts safe access with direct assignment or method calls.
What you can do after
Apply explicit runtime checks to use unknown values safely and explain when broader validation strategies are needed.

Read how Acing the Frontend Interview explains type guard →

Related concepts

Questions about type guard

What is type guard?

A type guard is a runtime-checkable condition that enables TypeScript to narrow an expression to a more specific type within a code path.

Which Manning books explain type guard?

Essential TypeScript 5, Third Edition and Acing the Frontend Interview.

What is the difference between type guard and non-null assertion operator?

A type guard is a runtime-checkable condition that enables TypeScript to narrow an expression to a more specific type within a code path. The non-null assertion operator is a TypeScript postfix exclamation-mark operator that asserts an expression is neither null nor undefined without performing a runtime check.

What kinds of type guard are there?

Kinds of type guard include switch statement.

What is type guard a kind of?

Type guard is a kind of TypeScript feature. A TypeScript feature is a language capability provided by TypeScript beyond the corresponding JavaScript language.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.