Bird
Raised Fist0
Typescriptprogramming~20 mins

Non-distributive conditional types in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Non-distributive Conditional Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a distributive conditional type
What is the output type of the following TypeScript code?
Typescript
type A<T> = T extends string ? "yes" : "no";
type Result = A<string | number>;
Astring | number
B"yes" | "no"
C"no"
D"yes"
Attempts:
2 left
💡 Hint
Conditional types distribute over unions by default.
❓ Predict Output
intermediate
2:00remaining
Output of a non-distributive conditional type using tuple wrapping
What is the output type of this TypeScript code?
Typescript
type B<T> = [T] extends [string] ? "yes" : "no";
type Result = B<string | number>;
A"yes"
B"yes" | "no"
C"no"
Dstring | number
Attempts:
2 left
💡 Hint
Wrapping the type in a tuple disables distribution.
🔧 Debug
advanced
2:00remaining
Identify the error in a non-distributive conditional type
Which option causes a TypeScript error when used as a non-distributive conditional type?
Typescript
type C<T> = T extends string ? number : boolean;
Atype D<T> = [T] extends string ? number : boolean;
Btype D<T> = T extends string ? number : boolean;
Ctype D<T> = [T] extends [string] ? number : boolean;
Dtype D<T> = [T] extends [string] ? number : boolean | string;
Attempts:
2 left
💡 Hint
Check the syntax of tuple wrapping in conditional types.
🧠 Conceptual
advanced
2:00remaining
Why use tuple wrapping in conditional types?
What is the main reason to wrap a type parameter in a tuple in a conditional type?
ATo make the conditional type distribute over unions
BTo enforce the type parameter to be a tuple
CTo convert the type parameter into an array type
DTo prevent the conditional type from distributing over unions
Attempts:
2 left
💡 Hint
Think about how distribution works in conditional types.
❓ Predict Output
expert
3:00remaining
Output of a complex non-distributive conditional type with nested unions
What is the type of Result after running this TypeScript code?
Typescript
type E<T> = [T] extends ["a" | "b"] ? "match" : "no match";
type Result = E<"a" | "c">;
A"no match"
B"match" | "no match"
C"match"
Dnever
Attempts:
2 left
💡 Hint
Remember that tuple wrapping disables distribution and the whole union is checked at once.

Practice

(1/5)
1. What does a non-distributive conditional type in TypeScript do differently compared to a distributive conditional type?
easy
A. It converts all types to string before evaluation.
B. It always distributes the condition over each member of the union type.
C. It only works with primitive types, not unions.
D. It evaluates the condition on the entire union type as a whole, not on each member separately.

Solution

  1. Step 1: Understand distributive conditional types

    Distributive conditional types apply the condition to each member of a union type separately.
  2. Step 2: Understand non-distributive conditional types

    Non-distributive conditional types evaluate the condition on the entire union type as a single entity, not distributing over members.
  3. Final Answer:

    It evaluates the condition on the entire union type as a whole, not on each member separately. -> Option D
  4. Quick Check:

    Non-distributive = whole union check [OK]
Hint: Non-distributive means no splitting union, check whole type [OK]
Common Mistakes:
  • Thinking conditional always distributes over unions
  • Confusing non-distributive with type narrowing
  • Assuming it only works on primitives
2. Which syntax correctly defines a non-distributive conditional type in TypeScript?
easy
A. type NonDist<T> = T | string ? true : false;
B. type NonDist<T> = T extends string ? true : false;
C. type NonDist<T> = [T] extends [string] ? true : false;
D. type NonDist<T> = T & string ? true : false;

Solution

  1. Step 1: Recognize non-distributive syntax

    Wrapping the type parameter in a tuple, like [T], prevents distribution over unions.
  2. Step 2: Compare options

    type NonDist<T> = [T] extends [string] ? true : false; uses [T] extends [string], which is the correct non-distributive syntax. type NonDist<T> = T extends string ? true : false; distributes over unions. Options C and D are invalid conditional syntax.
  3. Final Answer:

    type NonDist<T> = [T] extends [string] ? true : false; -> Option C
  4. Quick Check:

    Tuple wrapping disables distribution [OK]
Hint: Wrap type in tuple [T] to prevent distribution [OK]
Common Mistakes:
  • Not wrapping type in tuple, causing distribution
  • Using invalid conditional syntax
  • Confusing union with intersection in condition
3. Given the code:
type Test<T> = T extends string ? 'yes' : 'no';
type Result = Test<'a' | number>;

What is the type of Result?
medium
A. 'yes' | 'no'
B. 'yes'
C. 'no'
D. Error

Solution

  1. Step 1: Understand distributive conditional types

    Since T is a union 'a' | number, the condition distributes over each member.
  2. Step 2: Evaluate each member

    'a' extends string is true, so 'yes'; number extends string is false, so 'no'. Combined result is 'yes' | 'no'.
  3. Final Answer:

    'yes' | 'no' -> Option A
  4. Quick Check:

    Distributes over union = 'yes' | 'no' [OK]
Hint: Conditional distributes over union unless wrapped [OK]
Common Mistakes:
  • Assuming whole union checked at once
  • Expecting only 'yes' or only 'no'
  • Confusing union with intersection
4. Identify the error in this non-distributive conditional type:
type NonDist<T> = T extends [string] ? true : false;
medium
A. It incorrectly checks if T extends a tuple containing string, not preventing distribution.
B. It correctly prevents distribution by wrapping T in a tuple.
C. It causes a syntax error because of missing brackets.
D. It always returns false regardless of T.

Solution

  1. Step 1: Analyze the condition

    The condition checks if T extends [string], a tuple type, not wrapping T.
  2. Step 2: Understand distribution prevention

    To prevent distribution, T itself must be wrapped in a tuple: [T] extends [string]. This code does not do that, so distribution is not prevented.
  3. Final Answer:

    It incorrectly checks if T extends a tuple containing string, not preventing distribution. -> Option A
  4. Quick Check:

    Wrap T, not string, to prevent distribution [OK]
Hint: Wrap T in tuple, not the type to check [OK]
Common Mistakes:
  • Wrapping the wrong side of extends
  • Confusing tuple with type wrapper
  • Assuming syntax error instead of logic error
5. How can you write a non-distributive conditional type that checks if a union type T is exactly string | number (no more, no less)?
hard
A. type Exact<T> = T extends string | number ? true : false;
B. type Exact<T> = [T] extends [string | number] ? ([string | number] extends [T] ? true : false) : false;
C. type Exact<T> = T extends [string | number] ? true : false;
D. type Exact<T> = [T] extends [string] | [number] ? true : false;

Solution

  1. Step 1: Prevent distribution with tuple wrapping

    Wrapping T in a tuple [T] prevents distribution over unions.
  2. Step 2: Use mutual assignability checks

    Check [T] extends [string | number] and [string | number] extends [T] to ensure exact match, no extra or missing types.
  3. Final Answer:

    type Exact<T> = [T] extends [string | number] ? ([string | number] extends [T] ? true : false) : false; -> Option B
  4. Quick Check:

    Tuple wrap + mutual extends = exact type check [OK]
Hint: Wrap T and check both extends directions for exact match [OK]
Common Mistakes:
  • Not preventing distribution, causing partial matches
  • Checking only one extends direction
  • Using incorrect tuple wrapping
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