Hint
Distributive conditional types apply when T is a naked type parameter (not wrapped in []). Wrapping breaks distribution: [T] extends [U] treats T as a whole, not distributing over union members. This distinction matters for utility types like Exclude.
// Simulate TypeScript distributive conditional types
// type ToArray<T> = T extends unknown ? T[] : never
// Applied to union: distributes over each member
function toArrayType(values) {
// Type-level: ToArray<string | number> = string[] | number[]
// Runtime: demonstrate the distribution
if (Array.isArray(values)) {
return values.map(v => [v]); // each element wrapped in array
}
return [values];
}
// string | number → distributes → string[] | number[]
// NOT: (string | number)[]
const stringArrays = toArrayType(['a', 'b', 'c']);
const singleNum = toArrayType(42);
console.log(JSON.stringify(stringArrays));
console.log(JSON.stringify(singleNum));
// Prevent distribution with tuple brackets: [T] extends [unknown]
// type ToArrayNonDist<T> = [T] extends [unknown] ? T[] : never
// ToArrayNonDist<string | number> = (string | number)[]
console.log(typeof stringArrays[0][0]);
console.log(typeof singleNum[0]);[["a"],["b"],["c"]] [42] string number
Explanation: Distribution means the conditional is applied to each union member separately. ToArray
Key Insight: Distributive conditional types apply when T is a naked type parameter (not wrapped in []). Wrapping breaks distribution: [T] extends [U] treats T as a whole, not distributing over union members. This distinction matters for utility types like Exclude.