TypeScript Type Guards Cheat Sheet
Covers narrowing with typeof, instanceof, the in operator, custom type predicates, and assertion functions for safer runtime checks.
2 PagesIntermediateApr 12, 2026
typeof & instanceof Guards
Built-in narrowing for primitives and class instances.
typescript
function format(value: string | number) { if (typeof value === 'string') { return value.toUpperCase(); // narrowed to string } return value.toFixed(2); // narrowed to number}class Dog { bark() {} }class Cat { meow() {} }function speak(animal: Dog | Cat) { if (animal instanceof Dog) { animal.bark(); // narrowed to Dog } else { animal.meow(); // narrowed to Cat }}
Custom Type Predicates
Write reusable functions that narrow a union with 'is'.
typescript
interface Fish { swim(): void; }interface Bird { fly(): void; }function isFish(pet: Fish | Bird): pet is Fish { // type predicate return (pet as Fish).swim !== undefined;}function move(pet: Fish | Bird) { if (isFish(pet)) { pet.swim(); // narrowed to Fish } else { pet.fly(); // narrowed to Bird }}
'in' Operator & Discriminated Unions
Narrow by property presence or by a shared literal discriminant.
typescript
interface Admin { role: 'admin'; permissions: string[]; }interface Member { role: 'member'; }function describe(user: Admin | Member) { if ('permissions' in user) { // 'in' narrows by property presence console.log(user.permissions); } if (user.role === 'admin') { // discriminant narrowing console.log(user.permissions); }}
Assertion Functions (TS 3.7+)
Throw-based narrowing that persists after the function call returns.
typescript
function assertIsString(val: unknown): asserts val is string { if (typeof val !== 'string') { throw new Error('Expected a string'); }}function process(input: unknown) { assertIsString(input); input.toUpperCase(); // narrowed to string after the assertion call}function assertDefined<T>(val: T): asserts val is NonNullable<T> { if (val === null || val === undefined) throw new Error('Value is required');}
Narrowing Techniques
The full toolbox TypeScript offers for narrowing a type.
- typeof guard- Narrows primitives: 'string' | 'number' | 'boolean' | 'undefined' | 'function' | 'object' | 'symbol' | 'bigint'
- instanceof guard- Narrows by prototype chain for class instances
- in operator- Narrows by checking whether a property exists on the object
- user-defined type predicate- function isX(v): v is X { ... }
- assertion functions- asserts val is T throws instead of returning a boolean
- discriminated union narrowing- Switching/branching on a shared literal property like 'kind' or 'type'
- Array.isArray()- Built-in guard that narrows unknown to an array type
- truthiness narrowing- if (value) removes null/undefined/''/0 from the type
Pro Tip
Type predicates (pet is Fish) are only as safe as the logic inside the function - TypeScript trusts your implementation completely and won't verify it, so a buggy type guard can silently produce unsound narrowing.
Was this cheat sheet helpful?
Previous
TypeScript Namespaces & Modules Cheat Sheet
Next
TypeScript Utility Types Deep Dive Cheat Sheet
Explore Topics
#TypeScriptTypeGuards#TypeScriptTypeGuardsCheatSheet#Programming#Intermediate#TypeofInstanceofGuards#CustomTypePredicates#InOperatorDiscriminatedUnions#AssertionFunctionsTS37#Functions#Testing#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance