Preventing distribution
Concept and syntax
Wrapping both sides of the extends comparison in one-element tuples prevents naked-parameter distribution. The union is considered as a whole. A resulting array may therefore contain different members of the original union.
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 Together<T> = [T] extends [unknown] ? T[] : never; type Result=Together<string|number>;const x:Result=[1,"tea"]; console.log(JSON.stringify(x));Expected output:
[1,"tea"]Common mistake
Intentionally invalid; compile separately and do not execute.
type Together<T> = [T] extends [unknown] ? T[] : never; type Result=Together<string|number>;const x:Result=[true];Expected diagnostic: TS2322. The element type remains string | number; boolean is outside it.
Practice and recap
Compare Together<never> with a distributive Wrap<never>.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Preventing distribution — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Preventing distribution is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Together<T> = [T] extends [unknown] ? T[] : never; type Result=Together<string|number>;const x:Result=[1,"tea"]; console.log(JSON.stringify(x));Which diagnostic and explanation match the rejected example under the lesson settings?
type Together<T> = [T] extends [unknown] ? T[] : never; type Result=Together<string|number>;const x:Result=[true];What is Together<never>?