union type
A type that allows one of several listed types or values, written with a vertical bar, such as int | None in Python or "small" | "large" in TypeScript.
In more detail
Think of each type as a set of values: a union is the combined set. Without narrowing you may only use what every member supports, so a type checker insists you handle the None case before using an int | None value as a number. Python hints accept the bar syntax from Python 3.10; older code writes Optional[int] or Union[int, None].
Examples
Variant props are unions of their allowed options, which is how VariantProps and theme types such as "light" | "dark" are typed.type Bump = "patch" | "minor" | "major" in the governance lesson.def find_user(name: str) -> str | None: ...type QueryParam = string | string[] | number | null | undefined;Related terms
- TypeScript — JavaScript with added type annotations, checked by a compiler before the code runs.
- VariantProps — A TypeScript helper from CVA that extracts a component's prop types from its cva() definition.
- tuple type — A fixed-length list where each position has its own meaning and type, such as [major, minor, patch].
- type hint — A note in a function heading or variable declaration saying what type a value should be, such as def total(prices: list[float]) -> float:.
- None value — Python's special value meaning nothing here, which is what a function returns if it has no return statement.
- mypy — A static type checker for Python that reads type hints and reports code whose types do not fit together, without running it.
- intersection type — A type written A & B that requires a value to satisfy both types at once, combining their properties.
- type narrowing — The compiler refining a broad type to a more specific one inside a block, after a check proves which case applies.
- discriminated union — A type made of several object shapes that share one literal field, such as status or type, whose value tells them apart.
- literal type — A type that allows exactly one value, such as the string "GET", the number 404 or true.
- types as sets — A way of thinking about types as sets of possible values, so unions are combined sets and intersections are overlaps.
Taught in
- Dark Mode Strategies: System Preference, Class & High-Contrast — Design Systems & Component Engineering
- Polymorphism & Slotted Composition: The asChild Pattern — Design Systems & Component Engineering
- System Governance: Changesets, Semantic Versioning & Deprecation — Design Systems & Component Engineering
- Type Hints — Hands-on Python: Complete Video Course & Book
- Literal Types: String, Number, Boolean Literals And const — Production TypeScript
- void, undefined And null: Exact Optional Property Types — Production TypeScript
- never: The Bottom Type And Exhaustive Switch Checking — Production TypeScript
- Type Aliases Versus Interfaces: Declaration Merging — Production TypeScript
- Optional Properties, Readonly And Index Signatures — Production TypeScript
- Union Types, Intersections And Property Conflicts — Production TypeScript
- Discriminated Unions: Tagged Variants And Pattern Matching — Production TypeScript
- Tuples: Fixed Length, Labeled Elements And Variadic Tuples — Production TypeScript
- Typeof Guards, Truthiness And Equality Narrowing — Production TypeScript
- The in Operator And Instanceof Narrowing — Production TypeScript
- Exhaustive Narrowing With never In Production Handlers — Production TypeScript
- Generic Interfaces, Classes And Type Aliases — Production TypeScript
- Generic Constraints: extends, keyof And Indexed Access — Production TypeScript
- Core Utilities: Partial, Required, Readonly, Pick And Omit — Production TypeScript
- Union Utilities: Exclude, Extract, NonNullable And Awaited — Production TypeScript
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript
- Conditional Types: Ternary Types And Type Inference With infer — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript
- The satisfies Operator: Safe Validation Without Widening — Production TypeScript
- The Spine Artifact: Generating Typed Models From OpenAPI — Production TypeScript