Skip to content

Union Distribution

The Course
•
25 min study

A conditional with a bare type parameter on the left, such as T extends string ? T : never, distributes over a union. Substitute each member separately and join the results. For string | number, substitute T twice: the string branch returns string, and the number branch returns never. Joining them gives string | never, which simplifies to string. Here never means no possible value, extending the exhaustiveness meaning you met in intermediate.

If the question is about the whole union, wrap both sides in tuples: [T] extends [string] ? true : false. The tuple wrapping prevents this per-member distribution. It does not create an array at runtime; this is entirely type syntax.

Work through a concrete substitution before memorizing a trick. KeepStrings<string | number> filters members. AllStrings<string | number> asks whether every possible member fits string and returns false. Built-in Extract and Exclude perform related filtering; use the standard names in application code once you understand their behavior.

A filter that accidentally discards an alternative can make a public API look narrower than the data actually is. Write both positive and negative compile cases when the distinction matters. Day 4 builds an event union with a different tool—mapped types followed by indexed access. Not every operation involving a union is conditional-type distribution.

Predict, check, and run

Write the output before running. Use Check types first, then Check & run. Types disappear at runtime: explain both what the compiler checks and what the code actually does. Each editor is an independent ES module; await asynchronous work explicitly.

Predict, then runTypeScript
// A bare T checks each union member separately.
type KeepStrings<T> = T extends string ? T : never;

// The tuple wrapper checks the union as one whole type.
type AllStrings<T> = [T] extends [string] ? true : false;

const selected: KeepStrings<string | number> = "Types";
const whole: AllStrings<string | number> = false;

console.log(selected, whole);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
Types false

Read the compiler error

Predict the rejected relationship before checking. Read the real diagnostic and its source location; fix the contract or the value instead of hiding it with an assertion.

Find and fix the type errorTypeScript
type KeepStrings<T> = T extends string ? T : never;

const title: KeepStrings<string | number> = 25;

console.log(title);

Enable JavaScript to edit and type-check this example.

Check your diagnosis

The key line is TS2322: Type 'number' is not assignable to type 'string'. The required type, string, is the filtered union after distribution, not the original string | number; the message shows you the result of the type-level computation.

Filtering leaves string, so a number is not accepted. Restore a string rather than asserting the filtered result.

Substitute each member before simplifying the union

A distributive conditional runs its type-level test separately for each member of a union. Write those substitutions out before compressing the expression. Never contributes no values, so removing it from a union does not create a fallback runtime value.

A wrapped conditional asks one question about the whole input type. Use a concrete mixed union to distinguish the two designs; testing only string or only number makes the difference invisible. The declarations below use assignments to let the checker verify your predicted types.

Substitute each member before simplifying the union: traceTypeScript
type KeepText<T> = T extends string ? T : never;

const a: KeepText<"ready" | 25 | "done"> = "ready";
const b: KeepText<"ready" | 25 | "done"> = "done";

console.log(a, b);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
ready done

The string members survive and the numeric member becomes never. The result is a literal union, not every possible string.

Substitute each member before simplifying the union: contrastTypeScript
type Each<T> = T extends string ? true : false;

type Whole<T> = [T] extends [string] ? true : false;

const one: Each<string | number> = true;
const another: Each<string | number> = false;
const all: Whole<string | number> = false;

console.log(one, another, all);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
true false false

Each yields true|false because its members take different branches. Whole yields only false. A boolean-looking output does not reveal this distinction without the annotated assignments.

Try the rule on new inputs

Try assigning true to Whole<string|number> and numeric text to KeepText. Explain the failed assignment by expanding the conditional, not by memorizing the error message.

Debug a plausible mistake

Exercise

Repair per-member union filtering

Keep only string members of a union, allowing title to be Read. The helper must also work for a pure number type, yielding never. Explain which syntax controls distribution.

Use Check types to inspect the genuine diagnostic before repairing the code. Keep the intended contract; do not bypass the checker with any, a broad assertion or an ignore comment.

Find and fix the type errorTypeScript
type TextOnly<T> = [T] extends [string] ? T : never;
const title: TextOnly<string | number> = "Read";

console.log(title);

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Check types reports TS2322: Type '"Read"' is not assignable to type 'never'. A required type of never means the conditional produced no members at all. That is the clue that the whole union was tested at once, when you wanted each member tested separately: string | number as a whole is not a string, so the entire result became never.

A tuple-wrapped test considers the entire union together; per-member filtering needs the other form.

Hint 2: a first step

Use a naked type parameter on the checked side when the contract calls for per-member filtering.

Show one solution
TypeScript
type TextOnly<T> = T extends string ? T : never;
const title: TextOnly<string | number> = "Read";

console.log(title);
Text
Read

The syntax that controls distribution is whether the checked type is a naked type parameter. T extends string distributes, so TextOnly<string | number> is evaluated as TextOnly<string> | TextOnly<number>, giving string | never, which is string. Wrapping both sides as [T] extends [string] asks about the whole union at once; string | number is not a string, so the result was never and "Read" was rejected. With a pure number the repaired version still yields never, as the brief requires.

Trace the contract before building

Distribution asks the conditional question separately for each union member when the checked type is a naked type parameter. Substitute each member, simplify its branch, then join the remaining results. Never disappears from the resulting union because it contributes no possible value.

Wrapping the checked type in a tuple asks a different question about the whole union. That can be intentional, so choose based on the contract rather than treating wrapping as a universal fix. Compare a single matching type, a single nonmatching type, and a mixed union. The mixed case is what distinguishes member-by-member filtering from testing the union as a whole.

Build it yourself

Exercise

Union Distribution: practice

Write KeepNumbers<T> to retain numeric members, plus AllNumbers<T> to test a whole union. Print 25 and false for number|string. Do not use a runtime cast.

Your workspaceTypeScript
// Start from the stated contract. Add a valid call and an edge case.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

A filter returns T or never; a whole-union predicate returns literal booleans.

Hint 2: a first step

Only the whole-union test should wrap T in a tuple.

Show one solution
One possible solutionTypeScript
type KeepNumbers<T> = T extends number ? T : never;

type AllNumbers<T> = [T] extends [number] ? true : false;

const chosen: KeepNumbers<number | string> = 25;
const all: AllNumbers<number | string> = false;

console.log(chosen, all);

Enable JavaScript to edit and type-check this example.

Text
25 false

KeepNumbers checks a naked T, so it runs once per member and keeps only number, which accepts 25. AllNumbers wraps T in a one-element tuple, which turns distribution off: the question becomes "is the whole number | string a number?", and the answer is the literal false. Swap the two bodies and both assignments fail, which is a quick way to test which behavior you wrote.

Change it and test it

Test number alone and number|string first. Write the result of each branch before checking.

Going further

Try never itself only after the ordinary union cases make sense. Never is the empty union: a distributive filter has no members to process, so produces never; the wrapped test instead checks assignability of the whole tuple. This edge case is optional; later exercises use concrete event maps and do not depend on it.

Was this article helpful?

Your response is saved on this device.