Skip to content

Unions and intersections

When Relay boots with a surprising port, the first question is “where did that come from?”. relay config --explain answers it:

Terminal window
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.

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.

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 flag
const 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.

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 missing
const 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.

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 never
const 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.

const a: (string | number)[] = ["8080", 8080] // each element may be either
const b: string[] | number[] = ["8080", "9000"] // all strings, or all numbers
// @ts-expect-error: a string[] | number[] can't mix
const c: string[] | number[] = ["8080", 8080]

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.