Unions and intersections
When Relay boots with a surprising port, the first question is “where did that come from?”.
relay config --explain answers it:
port = 3000 (env PORT)queue = redis:6379 (flag --queue)timeout = 5000 (default)A source is one of three things, each with its own details. A setting is a key and a value and a source. Those two words, “or” and “and”, are unions and intersections.
Unions: one of several types
Section titled “Unions: one of several types”A | B is a value that is an A or a B. On a union, you can only use what every member has,
until you check which member you have:
function show(value: string | number) { value.toString() // fine: both have toString
// @ts-expect-error: toUpperCase does not exist on number value.toUpperCase()
if (typeof value === "string") { return value.toUpperCase() // fine: value is string here } return value.toFixed(0)}That if is narrowing, and the next lesson covers every way to do it.
Unions of object types
Section titled “Unions of object types”A union of objects describes data that comes in several shapes. Give each shape a literal field with a different value, and that field tells you which shape you’re holding:
type Source = | { readonly from: "env"; readonly variable: string } | { readonly from: "flag"; readonly flag: string } | { readonly from: "default" }
function where(source: Source): string { // @ts-expect-error: 'variable' does not exist on { from: "flag" ... } or { from: "default" } source.variable
if (source.from === "env") return `env ${source.variable}` // narrowed to the env member if (source.from === "flag") return `flag ${source.flag}` return "default"}
// @ts-expect-error: the "flag" shape requires a flagconst bad: Source = { from: "flag" }from is the discriminant. Each check on it removes members from the union, so by the last
return only { from: "default" } is left. M2 builds a whole domain model on this pattern.
Intersections: all of several types
Section titled “Intersections: all of several types”A & B is a value that is an A and a B. For object types, that means it has every property
of both:
type Source = { readonly from: "env"; readonly variable: string } | { readonly from: "default" }
type Setting = { readonly key: string; readonly value: string } & { readonly source: Source }
const s: Setting = { key: "port", value: "3000", source: { from: "env", variable: "PORT" } }
// @ts-expect-error: property 'source' is missingconst bad: Setting = { key: "port", value: "3000" }An intersection is how you attach the same extra fields to several types. Here the source is attached to a plain key/value pair.
When an intersection is never
Section titled “When an intersection is never”Intersecting types that share no values gives never, the type with no values at all:
type Impossible = string & number // never
type A = { port: number }type B = { port: string }type Both = A & B // { port: never }: no value can satisfy it
// @ts-expect-error: no value is assignable to neverconst both: Both = { port: 8080 }The second case is the dangerous one. A & B is accepted where it’s written, and the error only
shows up when you try to build a value. Lesson 8 shows how interface ... extends reports the
conflict where it happens instead.
Union or array of unions?
Section titled “Union or array of unions?”const a: (string | number)[] = ["8080", 8080] // each element may be eitherconst b: string[] | number[] = ["8080", "9000"] // all strings, or all numbers
// @ts-expect-error: a string[] | number[] can't mixconst c: string[] | number[] = ["8080", 8080]Exercise
Section titled “Exercise”pick resolves one setting and explain prints it. The compiler finds a source with a missing
field and a property that only one member of the union has. The tests find a precedence bug.
A | B: one of. Only shared members are usable until you narrow.- A literal discriminant field (
from,_tag,kind) lets each check select one member. A & B: all of. Use it to attach fields to another type.- Conflicting intersections become
never, and the error appears where you build a value.
Further reading
Section titled “Further reading”- Handbook: Everyday Types: union types.
- Handbook: Narrowing: narrowing a union.