0
0
Typescriptprogramming~10 mins

Non-distributive conditional types in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Non-distributive conditional types
Start with type T
Check if T extends U?
Return X
Wrap with [T
Result
This flow shows how wrapping a type in a tuple [T] prevents conditional types from distributing over unions.
Execution Sample
Typescript
type NonDist<T> = [T] extends [string] ? true : false;
type A = NonDist<string | number>;
// A is false, not true | false
This code checks if a type T is string without distributing over union types.
Execution Table
StepType TCondition [T] extends [string]?Branch TakenResult
1string | numberIs [string | number] assignable to [string]?Nofalse
Exitstring | numberCondition false, no distributionReturn falsefalse
💡 The condition fails because [string | number] is not assignable to [string], so the result is false without distribution.
Variable Tracker
VariableStartAfter Step 1Final
Tstring | numberstring | numberstring | number
Condition ResultN/Afalsefalse
ResultN/Afalsefalse
Key Moments - 2 Insights
Why doesn't the conditional type distribute over the union string | number?
Because T is wrapped in a tuple [T], TypeScript treats it as a single type, preventing distribution. See execution_table step 1 where condition checks [string | number] as a whole.
What would happen if we removed the tuple wrapping [T]?
The conditional type would distribute over each union member separately, resulting in true | false instead of a single false. This is shown by contrasting with the non-wrapped case.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the condition being checked?
AIs string assignable to string?
BIs string assignable to number?
CIs [string | number] assignable to [string]?
DIs number assignable to string?
💡 Hint
Check the 'Condition [T] extends [string]?' column at step 1 in execution_table.
At which step does the conditional type decide the result without distribution?
AStep 1
BStep 2
CExit step
DNo step, it distributes
💡 Hint
Look at the 'Branch Taken' and 'Result' columns in execution_table at step 1.
If we remove the tuple wrapping [T], what would the result type be for string | number?
Atrue
Btrue | false
Cfalse
Dnever
💡 Hint
Recall that without wrapping, conditional types distribute over unions producing a union of results.
Concept Snapshot
Non-distributive conditional types:
Use [T] extends [U] to prevent distribution over unions.
Without wrapping, conditional types distribute over each union member.
Example: type ND<T> = [T] extends [string] ? true : false;
ND<string | number> is false, not true | false.
This helps control type behavior precisely.
Full Transcript
This lesson shows how conditional types in TypeScript normally distribute over union types, but wrapping the type parameter in a tuple prevents this. The example type NonDist<T> uses [T] extends [string] to check if T is string without splitting unions. When T is string | number, the condition fails as a whole, returning false instead of true | false. This is because the tuple wrapping disables distribution. Key points include understanding how wrapping affects conditional checks and how it changes the resulting type. The execution table traces the condition check and result. The quiz tests understanding of the condition, step of decision, and effect of removing the tuple wrapping.