Программная инжененрия для JavaScript, TypeScrip, Node.js
👉 Group: https://t.me/How_Programming_Works
👉 Node.js channel: https://t.me/metarhia
👉 Node.js group: https://t.me/nodeua
Post #1683
11.1K
⭐️ Good and bad cases for TypeScript union types based on JavaScript V8 optimizations
👍 Good cases for union types:
- Union of strings instead of enum:
- Union of numeric as status or result code: type
- Union with shared properties:
- Union with common method:
👎 Bad cases for union types:
- Polymorphic object shapes causing depots:
- Requiring extensive "if"-logic and type checking:
- Inconsistent return types:
- Mixed primitives and objects:
- Сontradictory members:
- Union types that include any:
- Incompatible contracts:
🎁 Empty value for primitive types and reference types:
- Use
- Use
- Avoid mixing symbols with other types in unions
👍 Good cases for union types:
- Union of strings instead of enum:
type Direction = 'north' | 'south' | 'east' | 'west';- Union of numeric as status or result code: type
StatusCode = 200 | 201 | 204 | 400 | 500;- Union with shared properties:
type MailTarget = User | Company; (both with email)- Union with common method:
type Thenable = Promise | Query; (both with then method)👎 Bad cases for union types:
- Polymorphic object shapes causing depots:
type Something = User | Socket | string;- Requiring extensive "if"-logic and type checking:
type Input = string | number | boolean;- Inconsistent return types:
function getData(id: number): string | string[];- Mixed primitives and objects:
type Value = number | { value: number };- Сontradictory members:
type Person = { name: string; } | { name: number[] };- Union types that include any:
type FlexibleType = number | any;- Incompatible contracts:
type Handler = (() => string) | ((event: Event, data: any) => void);🎁 Empty value for primitive types and reference types:
- Use
null for empty reference types: Object, Function, Array, etc...- Use
undefined for empty primitive types: string, number, boolean, bigint- Avoid mixing symbols with other types in unions
- 👍 36
- ❤ 9
- 🤯 2
- 🔥 1





