TypeScript Narrowing and Type Guards Cheat Sheet
A 2-page reference for narrowing and type guards: 14 short entries with copy-ready code.
ForDevelopers
// typeof guard
function f(x: string | number) {
if (typeof x === "string") return x.toUpperCase();
return x.toFixed(2);
}
// Truthiness narrowing
function len(s?: string) {
return s ? s.length : 0;
}
// Equality narrowing
function g(x: "a" | "b") {
if (x === "a") {
// x is "a"
}
}
// in operator
type Cat = { meow(): void };
type Dog = { bark(): void };What's inside
- Format
- PDF, Markdown
- Contents
- 14 sections · 2 pages
- Version
- 1.0 · Updated 3 Oct 2026
- Licence
- Commercial What this means
- Checked
- Checked before it ships
- typescript-narrowing-and-type-guards-cheat-sheet.pdfPDF · 21 KB
- typescript-narrowing-and-type-guards-cheat-sheet.mdMarkdown · 2 KB
- LICENSE.txtadded to every download
About this item
A compact, printable reference for narrowing and type guards. Each of the 14 entries has a one-line explanation and a small example you can paste into your editor. The PDF uses a two-column A4 layout designed to sit next to your keyboard, and the same content ships as Markdown so you can search it or drop it into your notes. Every TypeScript example targets TypeScript 5.
- Type guards
- TypeScript
- Narrowing
- Types
- Cheat sheet
Reviews
No reviews yet.