Discriminated Unions

Discriminated Unions
πŸ‘¨β€πŸ’Ό Discriminated unions (also called "tagged unions" or "algebraic data types") are a pattern where each type in a union has a common "discriminant" property with a literal type value.
type Circle = { kind: 'circle'; radius: number }
type Rectangle = { kind: 'rectangle'; width: number; height: number }
type Shape = Circle | Rectangle
The kind property lets TypeScript narrow the type automatically. This is how you make invalid states unrepresentableβ€”the type system ensures you can only create valid combinations.
🐨 Open
index.ts
and:
  1. Create discriminated unions for API responses
  2. Create discriminated unions for payment methods
  3. Handle all cases with exhaustiveness checking

Please set the playground first

Loading "Discriminated Unions"
Loading "Discriminated Unions"
Login to get access to the exclusive discord channel.
Loading Discord Posts