Union type is a type that allows a value to be one of several possible types.
For example, in Typescript:
type Id = string | numberThis means an Id can be either a string or a number.
Union types are useful for:
- Modeling values with several valid shapes
- Representing state
- Handling values from dynamic systems
- Making invalid cases harder to express
Union types are often combined with literal types to model a fixed set of allowed values.
Anki
id: union-type-definition deck: Computer Science::Data Types tags: data-types union-type
Q: What is a union type? A: A union type allows a value to be one of several possible types.
id: union-type-typescript-example deck: Computer Science::Data Types tags: data-types union-type typescript
Q: What does type Id = string | number mean in TypeScript?
A: It means an Id can be either a string or a number.