Nâng CaoTypeScript iconTypeScript

Discriminated unions vs class hierarchy khi nào dùng cái nào?

Discriminated unions: prefer khi data-centric, cần exhaustive matching, serializable (Redux actions, API responses).

  • Redux Toolkit createSlice actions 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.

Xem toàn bộ TypeScript cùng filter theo level & chủ đề con.

Mở danh sách TypeScript