HardConditional Types💻 Output Question

Distributive conditional — union members resolved independently

💡

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.

What does this output?

// 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]);

Correct Output

[["a"],["b"],["c"]]
[42]
string
number

Why this output?

Explanation: Distribution means the conditional is applied to each union member separately. ToArray = ToArray | ToArray = string[] | number[], NOT (string | number)[].

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.

More Conditional Types Output Questions

MediumIsArray conditional — resolves based on type checkMediumNonNullable conditional — null and undefined are removedHardinfer extracts the wrapped type from a generic container

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz