liveBook · Manning

Union type

A union type is a type whose values can be any one of several specified alternative types or variants.

How 10 Manning books explain it

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.

Read how Pro Angular 16 explains union type →

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 domain alternatives where exactly one named case can exist, complementing records and tuples for coexisting data.
How deep it goes
Explains declaration, construction, pattern matching, nesting, and exhaustiveness, then applies unions to domain states and errors.
Examples
Uses customer contact methods, local versus international SMS numbers, loan decisions, validation errors, and musical instruments.
What you can do after
Apply unions to encode bounded domain alternatives and use exhaustive pattern matches to handle each case safely.

Read how F# in Action: A Revised Edition of Get Programming with F# 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
Models domain concepts whose valid values fall into distinct alternative cases.
How deep it goes
Defines sum types as ADT components and illustrates variant declarations without detailing pattern matching or representation internals.
Examples
Illustrates an Actions type with SendMoney, InviteToParty, and ScheduleMeeting variants.
What you can do after
Evaluate/understand how alternative variants can encode different forms of a domain action.

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.

Read how React in Depth 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.

Read how Modern C, Third Edition explains union type →

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.

Read how Modern C explains union type →

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.

Read how Python How-To explains union type →

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.

Read how Learn Haskell by Example explains union type →

Related concepts

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.