Skip to content

TypeScript Type Guards ​

Narrow a wide type safely, and get a compile error when you forget a case.

In this chapter: typeof and instanceof · the in operator · value is T predicates · discriminated unions · exhaustiveness with never

💡 The Core Idea ​

A type guard is a runtime check the compiler understands. That pairing is the whole point. typeof value === 'string' is a real check that survives compilation, and TypeScript uses it to narrow the type inside the branch. A wide type might be unknown, a union or an API payload. A guard is the only honest way to move from one to a specific type, because only a guard verifies anything. An as assertion narrows the type and checks nothing.

How It Works ​

GuardNarrowsReach for it when
typeof x === 'string'PrimitivesUnions of primitives
x instanceof ErrorClass instances, Date, Error, ArrayError handling, class hierarchies
'permissions' in userObject shapes by key presenceUnions without a discriminant
x.kind === 'circle'Discriminated unionsAny union you control the shape of
isUser(x) returning x is UserAnythingBoundaries, and reusable checks

typeof, and the null trap ​

typescript
function format(value: string | number): string {
  if (typeof value === 'string') return value.toUpperCase(); // string here
  return value.toFixed(2); // number here
}

typeof null is 'object', so a null slips into any object branch:

typescript
if (typeof value === 'object') { /* ❌ null reaches here */ }
if (value !== null && typeof value === 'object') { /* ✅ */ }

instanceof ​

The most common use is error handling, because a catch binding is unknown:

typescript
function describe(err: unknown): string {
  if (err instanceof ApiError) return `API ${err.statusCode}: ${err.message}`;
  if (err instanceof Error) return err.message; // ApiError is also an Error, so order matters
  return 'Unknown error';
}

⚠️ instanceof compares prototype chains, so it fails across realms (separate JavaScript environments). A Date from an iframe or a Node worker is not instanceof your Date. It also fails for a class extending a built-in when compiled to ES5 without an explicit Object.setPrototypeOf.

in ​

if ('permissions' in user) narrows a union by key presence, and inside the branch user.permissions is available. It helps where you cannot add a discriminant, for example on a third-party union. A discriminant is better where you own the type.

Type predicates ​

A function returning value is T becomes a reusable guard. This is how you validate an external payload once and narrow everywhere:

typescript
function isUser(value: unknown): value is User {
  if (typeof value !== 'object' || value === null) return false;
  const v = value as Record<string, unknown>;
  return typeof v.id === 'number' && typeof v.name === 'string';
}

const raw: unknown = await fetchJson('/api/users/1');
if (!isUser(raw)) throw new Error('Unexpected response shape');
raw.name; // User

The predicate is a promise you make to the compiler, not one it checks. A guard whose body is wrong is as unsafe as a cast. Write the body to match the type exactly, or use a schema library that derives both from one declaration.

Predicates are also what makes filter narrow:

typescript
const items: (User | null)[] = [/* … */];

const stillMaybe = items.filter((i) => i !== null); // ❌ (User | null)[]

function isNotNull<T>(value: T | null): value is T {
  return value !== null;
}
const users: User[] = items.filter(isNotNull); // ✅

Discriminated unions and exhaustiveness ​

tsx
type RequestState<T> =
  | { status: 'loading' }
  | { status: 'success'; data: T }
  | { status: 'error'; error: string };

function render(state: RequestState<User>): JSX.Element {
  switch (state.status) {
    case 'loading':
      return <Spinner />;
    case 'success':
      return <UserCard user={state.data} />; // `data` exists only here
    case 'error':
      return <ErrorBanner message={state.error} />;
    default: {
      // Add a fourth state and this line fails to compile
      const exhaustive: never = state;
      return exhaustive;
    }
  }
}

The never assignment in default is the highest-value four lines in this chapter. It turns "someone added a case and forgot to handle it" from a production bug into a build failure.

When to Use It ​

ScenarioReach forWhy
A union of primitivestypeofDirect, and the compiler knows it
A catch bindinginstanceof, most specific firstThe binding is unknown; nothing else narrows it
An API response, localStorage, postMessageA predicate, or a schema libraryThe shape is a runtime fact
A union you designedA literal discriminant fieldEnables switch narrowing and exhaustiveness
Filtering nulls out of an arrayA generic value is T predicatePlain filter does not narrow

Common Mistakes ​

❌ Using as where you meant a check. data as User silences the compiler and verifies nothing. Every runtime shape bug in a "fully typed" codebase starts here.

❌ Checking typeof x === 'object' before ruling out null.

❌ Writing a predicate whose body does not match its claim. return typeof v === 'object' inside an is User guard claims far more than it checks, and the compiler believes it.

❌ Ordering instanceof checks from general to specific. err instanceof Error first means the ApiError branch is dead code.

❌ Expecting narrowing to survive a callback. TypeScript drops narrowing across a function boundary, because the value could change in between:

typescript
if (user.avatar !== undefined) {
  setTimeout(() => load(user.avatar)); // ❌ possibly undefined again
}
const avatar = user.avatar; // ✅ capture in a const first

🔑 Key Takeaways ​

  • A type guard is a runtime check the compiler understands; an as assertion is neither.
  • typeof null is 'object', so rule out null before narrowing to an object.
  • A value is T predicate is a promise you make, not one the compiler verifies — write the body to match.
  • Plain filter does not narrow; a predicate does.
  • A never assignment in default turns an unhandled union member into a compile error.

Interview Questions ​

Q: What is a type predicate, and what is its main risk?

A function whose return type is value is T. When it returns true, the compiler narrows the argument in the calling branch. The risk is that the compiler trusts the signature without checking the body. An incomplete check gives you the same false confidence as a cast. That is why guards generated from a schema are safer than hand-written ones.

Q: How does exhaustiveness checking work?

Narrowing removes handled members from a union, so in the default branch the value's type is the union of everything unhandled. Assigning it to never succeeds only when nothing is left. Add a member and the assignment fails to compile at that line. The error points straight at the switch that needs updating.

Q: Why does narrowing not survive into a callback?

Because the compiler cannot prove the value is unchanged between the check and the callback running. Another function could reassign the property. Capturing the narrowed value in a const fixes it: a const cannot be reassigned, so the narrowing holds.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.