Distributivity
Concept and syntax
A conditional whose checked type is a naked type parameter distributes over union members. Each member is evaluated independently, and the results are unioned. Consequently this transformation accepts homogeneous arrays, not necessarily a mixed array.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type Wrap<T> = T extends unknown ? T[] : never; type Result=Wrap<string|number>;const x:Result=[1,2]; console.log(JSON.stringify(x));Expected output:
[1,2]Common mistake
Intentionally invalid; compile separately and do not execute.
type Wrap<T> = T extends unknown ? T[] : never; type Result=Wrap<string|number>;const x:Result=[1,"tea"];Expected diagnostic: TS2322. The result is string[] | number[], not (string | number)[].
Independent comparison
Tuple wrapping accepts mixed arrays here. Expected: '[1,"tea"]\n'
type W<T>=[T] extends [unknown]?T[]:never; const x:W<string|number>=[1,"tea"]; console.log(JSON.stringify(x));Practice and recap
Apply Wrap to a three-member union and enumerate the branches.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Distributivity — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Distributivity is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Wrap<T> = T extends unknown ? T[] : never; type Result=Wrap<string|number>;const x:Result=[1,2]; console.log(JSON.stringify(x));Which diagnostic and explanation match the rejected example under the lesson settings?
type Wrap<T> = T extends unknown ? T[] : never; type Result=Wrap<string|number>;const x:Result=[1,"tea"];What type does Wrap<boolean | 0> describe?