Tagged Unions and Exhaustive Type Checking
Learn how to use discriminated unions in TypeScript to handle multiple types safely. This module explains tagged unions, switch-based handling, and exhaustive type checking with practical examples.
1. Tagged Unions
Discriminated unions, also called tagged unions, use a common property to differentiate between multiple object types in a union.
Basic Example
The kind property serves as a tag to identify the type of shape.
2. Switch-Based Handling
Tagged unions are often handled using switch statements, ensuring type-safe property access.
Example
TypeScript narrows the type based on the kind property within each case.
Example with Functions
Switch-based handling ensures only valid properties are accessed for each type.
3. Exhaustive Type Checking
Exhaustive type checking ensures that all possible cases of a discriminated union are handled. This improves code safety and prevents runtime errors.
Example
The assertNever function catches any unhandled cases at compile-time.
Conclusion
Discriminated unions in TypeScript provide a powerful pattern for handling multiple related types safely. Tagged properties, switch-based handling, and exhaustive type checking ensure type safety, reduce errors, and improve code maintainability in complex applications.