Union type
A union type is a type whose values can represent one of several specified alternative types or variants.
Essential TypeScript 5, Third Edition
A union type is a TypeScript type that allows a value to conform to any one of multiple constituent types and exposes only members shared by those types.
- How it is used
- Allows variables, function results, parameters, and arrays to accept several specified types while retaining type checking.
- How deep it goes
- Works through syntax, shared members, property types, narrowing, nullable values, and limits that motivate overloads or generics.
- Examples
- Uses a tax function returning strings or numbers, nullable tax results, and an array mixing objects with shared properties.
- What you can do after
- Build it from scratch: annotate mixed values and safely access constituent-specific members after narrowing.
Read how Essential TypeScript 5, Third Edition explains union type →
Pro Angular 16
A union type is a TypeScript type that permits a value to belong to one of several specified types.
- How it is used
- Restricts function parameters, variables, and mixed array elements to specified alternatives without using `any`.
- How deep it goes
- Works through annotations, compiler errors, type checks, missing values, and array grouping; stops short of compiler internals.
- Examples
- Uses a number-or-string function, a nullable variable, an optional parameter, and a mixed-type array.
- What you can do after
- Apply it via TypeScript annotations to permit selected alternatives and check a value before using type-specific methods.
F# in Action: A Revised Edition of Get Programming with F#
A discriminated union is an F# type whose values are selected from a fixed set of named cases, each potentially carrying different data.
- How it is used
- Models mutually exclusive domain choices where records and tuples model data that exists together.
- How deep it goes
- Works through case declarations, construction, nested payloads, and exhaustive matching without detailing runtime internals.
- Examples
- Uses customer contact methods, nested SMS numbers, and loan decisions to illustrate cases.
- What you can do after
- Build it from scratch to represent domain choices, then pattern-match their case-specific data and heed missing-case warnings.
Read how F# in Action: A Revised Edition of Get Programming with F# explains union type →
Data-Oriented Programming in Java
A sum type is a type formed by combining alternative types with OR semantics so that a value belongs to one of several permitted variants.
- How it is used
- Models domain choices whose alternatives belong to one named idea with OR semantics.
- How deep it goes
- Defines Java sealed-interface representation, counts variants, and previews algebraic refactoring while deferring set-theoretic machinery and detailed practice.
- Examples
- Uses checklist statuses, Coffee and Tea, Optional-style emptiness, and recursive Rule variants for equality and logical operators.
- What you can do after
- Build it from scratch as a sealed-interface hierarchy that makes a closed set of domain alternatives explicit.
Read how Data-Oriented Programming in Java explains union type →
Functional Design and Architecture: Examples in Haskell
A sum type represents a value that can be one of several alternative constructors or variants.
- How it is used
- Uses union types to construct types that reflect a domain in functional design.
- How deep it goes
- Introduces sum types informally and illustrates alternatives without a detailed implementation walkthrough.
- Examples
- Illustrates alternatives with an actions type and a recursive list with Cons and Nil constructors.
- What you can do after
- Evaluate and understand how alternatives and product types can represent a domain.
Read how Functional Design and Architecture: Examples in Haskell explains union type →
Kafka Streams in Action, Second Edition
A union type is a construct whose value or field may conform to one of several specified types.
- How it is used
- Keeps related Kafka event types on one topic while constraining values to a declared set.
- How deep it goes
- Explains schema declaration, reference registration, serializer settings, consumer configuration, and concrete-type dispatch.
- Examples
- Uses PlaneEvent, TruckEvent, DeliveryEvent, an all_events.avsc union, and a TransportationEvent record.
- What you can do after
- Operate Avro producers and consumers that serialize, retrieve, and dispatch multiple event types from one Kafka topic.
Read how Kafka Streams in Action, Second Edition explains union type →
React in Depth
A discriminated union is a TypeScript union type whose alternatives are distinguished by a shared property with distinct literal values.
- How it is used
- Constrains React component props so property values select valid configuration-specific requirements.
- How deep it goes
- Defines interfaces, unions them, narrows with literal checks, and notes that extracted rest properties lose discriminator correlation.
- Examples
- Uses an e-commerce product card with regular and on-sale variants, sale price, and expiry date.
- What you can do after
- Build it from scratch by modeling React component configurations and safely accessing variant-specific properties after narrowing.
Type-Driven Development with Idris
A data type whose constructors can carry associated data, with different constructors able to carry different data.
- How it is used
- Represents alternatives whose constructors carry different data, such as shapes with dimensions or commands with arguments.
- How deep it goes
- Defines constructors with argument types, demonstrates pattern matching, and shows constructor documentation through Idris’s :doc command.
- Examples
- Uses shapes with dimension data and parsed commands with string or integer arguments.
- What you can do after
- Build it from scratch by declaring constructors with their own argument types and matching on them in functions.
Read how Type-Driven Development with Idris explains union type →
Modern C, Third Edition
A C union is a user-defined type whose members occupy overlapping storage and are represented by the same memory location.
- How it is used
- Uses overlapping member storage to inspect an object's representation and examine individual bytes.
- How deep it goes
- Explains storage overlay, representation digits, sizeof sizing, and implementation-defined byte order, but stops short of broader union operations.
- Examples
- Uses an unsigned value initialized to 0xAABBCCDD and prints its overlaid bytes to reveal machine byte order.
- What you can do after
- Evaluate object representations and investigate implementation-defined byte order with a union and unsigned-char array.
Modern C
A union is a C type whose members share the same memory location, so different members overlay one another.
- How it is used
- Uses unions to inspect an object's individual bytes through an alternate type interpretation.
- How deep it goes
- Defines overlay semantics and works through byte output, while deferring broader alignment and representation limits.
- Examples
- Demonstrates an unsigned value overlaid with an unsigned-char array and prints its bytes in machine-dependent order.
- What you can do after
- Apply it via C union declarations to inspect an unsigned object's representation and compare observed byte order.
Acing the Frontend Interview
A union type allows a value or parameter to have one of several specified types.
- How it is used
- Frames union types as a TypeScript choice for simple function parameters with a fixed, predictable set of allowed types.
- How deep it goes
- Compares unions with generics and notes narrowing, but stops short of a worked union implementation.
- Examples
- Uses a function parameter accepting a string or number as the representative fixed-set example.
- What you can do after
- Evaluate function-parameter designs and choose unions or generics according to type-set size, relationships, and simplicity.
Python How-To
A union type specifies that a value may belong to one of several permitted data types.
- How it is used
- Documents function parameters that accept values from several data types.
- How deep it goes
- Explains vertical-bar syntax, nested container annotations, and IDE feedback without covering runtime validation.
- Examples
- Uses statistics over a list-or-tuple parameter, multiway annotations, and unions nested inside set or list annotations.
- What you can do after
- Apply it via Python type hints to document flexible function parameters and receive development-time incompatibility warnings.
Learn Haskell by Example
A sum type is a data type whose value is one of several alternative constructors, with only one constructor choice present at a time.
- How it is used
- Models distinguishable choices in a function interface, such as selecting left or right string padding.
- How deep it goes
- Defines constructors and demonstrates pattern matching through a complete padding function, without descending into runtime internals.
- Examples
- Demonstrates PadMode with PadLeft and PadRight, then applies both choices to padding strings.
- What you can do after
- Apply it via Haskell data declarations and case expressions to model and handle a fixed set of alternatives.
Also covered in
- Learn C++ by Example
- Functional Programming in Scala, Second Edition
- Write Powerful Rust Macros
- Go in Practice, Second Edition
- Designing AI SystemsIn early access (MEAP)
- 100 C++ Mistakes and How to Avoid Them
- Julia as a Second Language
- Secrets of the JavaScript Ninja, Third EditionIn early access (MEAP)
- Functional Programming in C#, Second Edition
- The Joy of JavaScript
- Embedded Software with RustIn early access (MEAP)
What to know first
- arrayAn array is an ordered collection of elements organized in one or more dimensions and accessible by position or index.
- functionA function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- stringA string is a programming-language value representing textual data as an ordered sequence of characters or encoded units.
- genericsGenerics are programming-language constructs parameterized by types, allowing definitions to apply to multiple concrete types while preserving type relationships.
Related concepts
- optionincludes · An option type is a type that represents either a value of a specified type or its absence as distinct alternatives.
- result typeincludes · A result type represents the outcome of an operation as either a successful value or a failure, often carrying information about either outcome.
- genericskind of · Generics are programming-language constructs parameterized by types, allowing definitions to apply to multiple concrete types while preserving type relationships.
- single-case discriminated unionincludes · A single-case discriminated union is a discriminated union with exactly one named case, typically used to create a distinct domain-specific type around another value.
- System.Text.Jsoncontrasts with · System.Text.Json is a .NET library that provides APIs for serializing, deserializing, reading, and writing JSON data.
- algebraic data typekind of · An algebraic data type is a composite type whose values are formed from product structures, finite alternatives, or both through type constructors.
Questions about union type
What is union type?
A union type is a type whose values can represent one of several specified alternative types or variants.
Which Manning books explain union type?
Essential TypeScript 5, Third Edition, Pro Angular 16, F# in Action: A Revised Edition of Get Programming with F#, Data-Oriented Programming in Java, Functional Design and Architecture: Examples in Haskell, Kafka Streams in Action, Second Edition, React in Depth, Type-Driven Development with Idris and 5 more.
What should I know before learning union type?
Manning books that teach union type usually introduce array, function, string and generics first.
What is the difference between union type and System.Text.Json?
A union type is a type whose values can represent one of several specified alternative types or variants. System.Text.Json is a .NET library that provides APIs for serializing, deserializing, reading, and writing JSON data.
What kinds of union type are there?
Kinds of union type include option, result type and single-case discriminated union.
What is union type a kind of?
Union type is a kind of generics. Generics are programming-language constructs parameterized by types, allowing definitions to apply to multiple concrete types while preserving type relationships.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.












