Union Type
Question
What does a union type A | B mean?
Hint
Union types represent values that can be one of several types.
Explanation
A union type A | B means a value can be of type A or type B. Example: let id: string | number. You can only access properties common to both types without narrowing. To access type-specific properties, use type guards (typeof, instanceof, in) or discriminant properties. Union types are extremely common for function parameters that accept multiple types, optional values (T | undefined), and results (T | null).
Related Resources
Related Lessons
- Unions & Intersections
Learn unions/intersections.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.