Discriminated unions: prefer khi data-centric, cần exhaustive matching, serializable (Redux actions, API responses).
- Redux Toolkit
createSliceactions là ví dụ thực tế điển hình — mỗi action type là một discriminated union member và reducers exhaustively switch qua chúng. - Class hierarchy: khi cần behavior với data, encapsulation, OOP patterns.
- TypeScript exhaustiveness checking qua never type.
Discriminated unions: prefer when data-centric, need exhaustive matching, or data must be serializable (Redux actions, API responses).
- Redux Toolkit
createSliceactions are a prime real-world example — each action type is a discriminated union member and reducers exhaustively switch through them. - Class hierarchy: when you need behavior bundled with data, encapsulation, or OOP patterns.
- TypeScript exhaustiveness checking uses the never type.