Codington
0 of 5 problems solved0%

Union & Litteral Types

5 exercises

Union types let a value be one of several possibilities, while literal types restrict values to exact constants:

type Status = 'idle' | 'loading' | 'success' | 'error'; type Id = string | number; let status: Status = 'loading'; // ✅ OK let status: Status = 'pending'; // ❌ Error

TypeScript performs control-flow narrowing: inside branches guarded by typeof, equality checks, or switch, the compiler refines a union to a specific member, giving you type safety and better autocompletion.

In this category, you'll:

  • Create unions of primitives (string | number)
  • Use literal types for statuses, directions, and modes
  • Narrow unions with typeof, equality checks, and switch
  • Work with discriminated unions (tagged with fields like kind or type)

This category focuses on practical everyday patterns: clear narrowing and small, well-defined state sets.

CodingtonCodington

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