Codington
0 of 6 problems solved0%

Type Guards & Narrowing

6 exercises

Type guards help the compiler figure out which shape a value has at a given point. When a check succeeds, TypeScript narrows the type so you can access the right properties safely:

function process(value: string | number) { if (typeof value === 'string') { return value.toUpperCase(); // ✅ TypeScript knows it's a string } return value.toFixed(2); // ✅ TypeScript knows it's a number }

Built-in type guards:

  • typeof – for primitives ('string', 'number', 'boolean')
  • instanceof – for classes (value instanceof Date)
  • in – for property checks ('name' in obj)
  • Array.isArray() – for arrays

Custom type guards:

function isUser(value: unknown): value is User { return typeof value === 'object' && value !== null && 'id' in value; }

In this category, you'll:

  • Narrow unions with built-in type guards
  • Write custom type predicates with value is Type
  • Use exhaustive switch statements with discriminated unions
  • Leverage the never type to catch unhandled cases

By the end, you'll be able to narrow any union type safely and teach TypeScript about your own runtime checks.

CodingtonCodington

© 2025 Codington. Built with TypeScript, TanStack Start and a lot of console.log().