Bird
Raised Fist0
Typescriptprogramming~10 mins

Distributive conditional types in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Distributive conditional types
Start with type T
↓
Is T a union?
↓
Final type
Distributive conditional types check each member of a union type separately, then combine the results.
Execution Sample
Typescript
type Example<T> = T extends string ? "yes" : "no";
type Result = Example<string | number>;
Checks each type in the union string | number; returns "yes" for string, "no" for number.
Execution Table
StepType TCondition (T extends string?)Result for TCombined Result
1stringstring extends string? Yes"yes""yes"
2numbernumber extends string? No"no""yes" | "no"
3Final--"yes" | "no"
💡 All union members processed, combined results form final type.
Variable Tracker
VariableStartAfter 1After 2Final
Tstring | numberstringnumber-
Result for T-"yes""no""yes" | "no"
Key Moments - 2 Insights
Why does the conditional type apply separately to each member of the union?
Because distributive conditional types automatically split the union and apply the condition to each member, as shown in execution_table rows 1 and 2.
What happens if the type is not a union?
The condition applies directly without splitting, as shown in the 'No' branch in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result for the type 'number' at step 2?
A"yes"
B"no"
C"number"
D"string"
💡 Hint
Check the 'Result for T' column at step 2 in the execution_table.
At which step does the combined result become "yes" | "no"?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the 'Combined Result' column in the execution_table rows.
If T was just 'string' (not a union), what would the combined result be?
A"yes" | "no"
B"no"
C"yes"
DError
💡 Hint
Refer to concept_flow 'No' branch and variable_tracker for single type behavior.
Concept Snapshot
Distributive conditional types split union types and apply conditions to each member.
Syntax: T extends U ? X : Y
If T is a union, condition applies to each member separately.
Results combine into a new union type.
If T is not a union, condition applies directly.
Useful for transforming union types based on conditions.
Full Transcript
Distributive conditional types in TypeScript work by checking if a type T is a union. If it is, TypeScript splits the union into each member and applies the conditional type to each member separately. For example, with type Example<T> = T extends string ? "yes" : "no"; and T = string | number, the condition applies to string first, returning "yes", then to number, returning "no". These results combine into the union "yes" | "no". If T is not a union, the condition applies directly without splitting. This behavior helps transform union types based on conditions. The execution table shows each step, the condition check, and the combined result. Beginners often wonder why the condition applies separately; it's because of this distributive behavior. Also, if the type is not a union, the condition applies once. Understanding this helps write powerful type transformations in TypeScript.

Practice

(1/5)
1. What does a distributive conditional type do in TypeScript?
T extends U ? X : Y when T is a union type?
easy
A. Applies the condition separately to each member of the union type
B. Applies the condition once to the entire union type as a whole
C. Always returns the true branch X regardless of T
D. Always returns the false branch Y regardless of T

Solution

  1. Step 1: Understand conditional types with unions

    When T is a union, TypeScript distributes the condition over each member separately.
  2. Step 2: Apply distributive behavior

    Each member of T is checked against U individually, returning X or Y per member.
  3. Final Answer:

    Applies the condition separately to each member of the union type -> Option A
  4. Quick Check:

    Distributive conditional types = separate checks per union member [OK]
Hint: Remember: conditional types split unions into parts [OK]
Common Mistakes:
  • Thinking the condition applies once to the whole union
  • Assuming it always returns true or false branch
  • Confusing distributive with non-distributive conditional types
2. Which of the following is the correct syntax for a distributive conditional type in TypeScript?
easy
A. type Result<T> = if (T extends string) number else boolean;
B. type Result<T> = T => string ? number : boolean;
C. type Result<T> = T ? number : boolean;
D. type Result<T> = T extends string ? number : boolean;

Solution

  1. Step 1: Recall correct conditional type syntax

    Conditional types use T extends U ? X : Y syntax in TypeScript.
  2. Step 2: Match syntax with options

    type Result<T> = T extends string ? number : boolean; matches the correct syntax; others use invalid or incorrect syntax.
  3. Final Answer:

    type Result<T> = T extends string ? number : boolean; -> Option D
  4. Quick Check:

    Conditional types use extends ? : syntax [OK]
Hint: Look for extends ? : pattern in type definitions [OK]
Common Mistakes:
  • Using if-else instead of conditional operator
  • Missing the extends keyword
  • Using arrow functions incorrectly in type definitions
3. Given the type:
type FilterString<T> = T extends string ? T : never;
type Result = FilterString<'a' | 1 | 'b' | true>;

What is the type of Result?
medium
A. never
B. 'a' | 1 | 'b' | true
C. 'a' | 'b'
D. string

Solution

  1. Step 1: Apply distributive conditional type to each union member

    Each member is checked: 'a' extends string (true), 1 extends string (false), 'b' extends string (true), true extends string (false).
  2. Step 2: Collect true branch results

    Only 'a' and 'b' remain; others become never and are removed from union.
  3. Final Answer:

    'a' | 'b' -> Option C
  4. Quick Check:

    FilterString filters strings only = 'a' | 'b' [OK]
Hint: Distributive types filter unions by returning never for false cases [OK]
Common Mistakes:
  • Thinking the whole union is returned unchanged
  • Confusing never with empty string
  • Assuming the result is just string instead of filtered literals
4. Identify the error in this distributive conditional type:
type ExtractNumber<T> = [T] extends [number] ? T : never;
type Result = ExtractNumber<'a' | 1 | 2>;
medium
A. The condition is wrapped in a tuple, so distribution does not occur
B. The syntax is invalid because of missing extends keyword
C. The type will always return never
D. The union type is not allowed in conditional types

Solution

  1. Step 1: Understand tuple wrapping effect

    Wrapping T in a tuple disables distributive behavior in conditional types.
  2. Step 2: Analyze impact on ExtractNumber

    Since distribution is off, the whole union is checked at once, so 'a' | 1 | 2 extends number is false, returning never.
  3. Final Answer:

    The condition is wrapped in a tuple, so distribution does not occur -> Option A
  4. Quick Check:

    Tuple wrapping disables distributive conditional types [OK]
Hint: Tuple brackets stop distribution in conditional types [OK]
Common Mistakes:
  • Assuming distribution still happens with tuple wrapping
  • Confusing syntax error with logic error
  • Thinking union types are not allowed in conditional types
5. How can you create a type that converts a union of types T into a union of their array types using distributive conditional types?
Choose the correct type definition.
hard
A. type ToArray<T> = T extends any[] ? T : never;
B. type ToArray<T> = T extends any ? T[] : never;
C. type ToArray<T> = T[];
D. type ToArray<T> = [T] extends [any] ? T[] : never;

Solution

  1. Step 1: Understand distributive conditional type for mapping

    Using T extends any ? T[] : never applies the array transformation to each union member separately.
  2. Step 2: Compare options

    type ToArray<T> = T extends any ? T[] : never; correctly distributes and converts each member to an array type. type ToArray<T> = [T] extends [any] ? T[] : never; disables distribution by tuple wrapping. type ToArray<T> = T[]; wraps the whole union as an array, not each member. type ToArray<T> = T extends any[] ? T : never; filters arrays, not converts.
  3. Final Answer:

    type ToArray<T> = T extends any ? T[] : never; -> Option B
  4. Quick Check:

    Distributive conditional types map unions member-wise [OK]
Hint: Use T extends any ? to distribute over unions [OK]
Common Mistakes:
  • Wrapping T in tuple disables distribution
  • Using T[] directly converts whole union, not members
  • Confusing filtering with mapping in conditional types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard