📖 Scenario: Imagine you have a list of different fruits and vegetables. You want to create a type that tells you if each item is a fruit or not. This helps you organize your shopping list better.
🎯 Goal: You will build a TypeScript type called IsFruit that checks if a given item is a fruit using distributive conditional types. Then, you will apply it to a union of items and see the results.
📋 What You'll Learn
Create a union type called Items with the values 'apple', 'carrot', and 'banana'.
Create a type called IsFruit that uses a distributive conditional type to check if a type extends 'apple' | 'banana'.
Create a new type called Result that applies IsFruit to Items.
Print the Result type to see which items are fruits.
💡 Why This Matters
🌍 Real World
Distributive conditional types help you create flexible and reusable type checks in TypeScript, which is useful when working with complex data structures.
💼 Career
Understanding distributive conditional types is important for TypeScript developers to write safer and more expressive code, especially in large projects or libraries.
Progress0 / 4 steps
1
Create the union type Items
Create a union type called Items with the exact values 'apple', 'carrot', and 'banana'.
Typescript
Hint
Use the | symbol to combine string literal types into a union.
2
Create the distributive conditional type IsFruit
Create a type called IsFruit that takes a generic type T and returns 'Yes' if T extends 'apple' | 'banana', otherwise returns 'No'. Use a conditional type with T extends 'apple' | 'banana' ? 'Yes' : 'No'.
Typescript
Hint
Use extends in a conditional type to check if T is one of the fruits.
3
Apply IsFruit to Items to create Result
Create a new type called Result that applies IsFruit to the union type Items.
Typescript
Hint
Apply the conditional type to the union to get a new union of results.
4
Print the Result type
Write a line to print the string 'Yes' | 'No' which represents the Result type. Use console.log with a string showing the expected union type.
Typescript
Hint
TypeScript types do not exist at runtime, so print the expected union as a string.
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
Step 1: Understand conditional types with unions
When T is a union, TypeScript distributes the condition over each member separately.
Step 2: Apply distributive behavior
Each member of T is checked against U individually, returning X or Y per member.
Final Answer:
Applies the condition separately to each member of the union type -> Option A
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
Step 1: Recall correct conditional type syntax
Conditional types use T extends U ? X : Y syntax in TypeScript.
Step 2: Match syntax with options
type Result<T> = T extends string ? number : boolean; matches the correct syntax; others use invalid or incorrect syntax.
Final Answer:
type Result<T> = T extends string ? number : boolean; -> Option D
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
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).
Step 2: Collect true branch results
Only 'a' and 'b' remain; others become never and are removed from union.
Final Answer:
'a' | 'b' -> Option C
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
Step 1: Understand tuple wrapping effect
Wrapping T in a tuple disables distributive behavior in conditional types.
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.
Final Answer:
The condition is wrapped in a tuple, so distribution does not occur -> Option A
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
Step 1: Understand distributive conditional type for mapping
Using T extends any ? T[] : never applies the array transformation to each union member separately.
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.
Final Answer:
type ToArray<T> = T extends any ? T[] : never; -> Option B