What is the main purpose of conditional types in TypeScript?
Conditional types allow you to choose one type or another based on a condition, similar to an if-else statement but for types. They help create flexible and reusable type logic.
Click to reveal answer
intermediate
How can conditional types replace function overloads?
Instead of writing multiple function signatures (overloads), conditional types let you define a single function with a type that changes based on input types, simplifying code and improving maintainability.
Click to reveal answer
beginner
Explain this conditional type: type Result<T> = T extends string ? number : boolean;
This type means: if T is a string, then Result<T> is number; otherwise, Result<T> is boolean. It picks the output type based on whether T is a string.
Click to reveal answer
intermediate
Why might conditional types be preferred over overloads in some cases?
Conditional types reduce repetition by combining multiple overloads into one type expression. This makes code easier to read, write, and maintain, especially when many similar overloads exist.
Click to reveal answer
beginner
What does the 'extends' keyword do in a conditional type?
In conditional types, 'extends' checks if one type fits within another. It acts like a test: if the left type can be assigned to the right type, the condition is true.
Click to reveal answer
What does this conditional type do? type Check<T> = T extends number ? 'num' : 'not num';
AReturns 'num' for all types
BReturns 'num' if T is number, else 'not num'
CReturns 'not num' for all types
DCauses a syntax error
✗ Incorrect
The conditional type checks if T is assignable to number. If yes, it returns 'num', otherwise 'not num'.
Which keyword is used to test a type condition in conditional types?
Aextends
Bimplements
Ctypeof
Dinstanceof
✗ Incorrect
'extends' is used in conditional types to check if one type fits within another.
How do conditional types help replace function overloads?
ABy forcing all inputs to be the same type
BBy removing the need for functions
CBy using multiple functions instead
DBy defining a single function with a type that changes based on input
✗ Incorrect
Conditional types allow one function signature to adapt its return type based on input types, replacing multiple overloads.
What is the output type of Result<string> if type Result<T> = T extends string ? number : boolean;?
Anumber
Bboolean
Cstring
Dnever
✗ Incorrect
Since T is string, the condition is true, so the type is number.
Which is NOT a benefit of using conditional types over overloads?
ALess repetitive code
BEasier to maintain
CMore runtime performance
DSimpler type definitions
✗ Incorrect
Conditional types affect compile-time types only and do not improve runtime performance.
Describe how conditional types work and how they can replace function overloads in TypeScript.
Think about how if-else logic applies to types.
You got /4 concepts.
Write a simple conditional type that returns 'string' if the input type is boolean, and 'number' otherwise.
Use T extends boolean ? string : number
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using conditional types in TypeScript for overload replacement?
easy
A. To enforce runtime type checking in JavaScript
B. To create multiple functions with the same name but different parameters
C. To select a type based on a condition, replacing multiple overloads with one type
D. To convert all types to strings automatically
Solution
Step 1: Understand conditional types
Conditional types allow choosing a type based on a condition, like an if-else for types.
Step 2: Relate to overload replacement
They replace multiple function overloads by selecting the correct return type based on input types.
Final Answer:
To select a type based on a condition, replacing multiple overloads with one type -> Option C
Quick Check:
Conditional types = overload replacement [OK]
Hint: Think 'if-else' but for types to replace overloads [OK]
Believing conditional types convert types to strings
2. Which of the following is the correct syntax for a conditional type in TypeScript?
easy
A. type Result = if (T extends U) { X } else { Y };
B. type Result = T == U ? X : Y;
C. type Result = T ? U : V;
D. type Result = T extends U ? X : Y;
Solution
Step 1: Recall conditional type syntax
Conditional types use the syntax: type Result = T extends U ? X : Y;
Step 2: Compare options
type Result = T extends U ? X : Y; matches the correct syntax. Options A, C, and D use invalid or incorrect syntax.
Final Answer:
type Result = T extends U ? X : Y; -> Option D
Quick Check:
Conditional type syntax = type Result = T extends U ? X : Y; [OK]
Hint: Remember: 'extends' with '?' and ':' for conditional types [OK]
Common Mistakes:
Using if-else syntax inside type definitions
Confusing ternary operator with conditional types
Using '==' instead of 'extends' keyword
3. Given the code below, what is the type of result?
type IsString<T> = T extends string ? "Yes" : "No";
let result: IsString<number>;
medium
A. "Yes"
B. "No"
C. number
D. string
Solution
Step 1: Analyze the conditional type
The type IsString<T> returns "Yes" if T extends string, else "No".
Step 2: Apply to number
Since number does not extend string, the type is "No".
Final Answer:
"No" -> Option B
Quick Check:
number extends string? No [OK]
Hint: Check if type extends string; if not, result is "No" [OK]
Common Mistakes:
Assuming number extends string
Confusing type with value
Ignoring the conditional type logic
4. Identify the error in this conditional type usage:
type Check<T> = T extends number ? "Num" : "NotNum";
function test<T>(value: T): Check<T> {
if (typeof value === "number") {
return "Num";
} else {
return "NotNum";
}
}
medium
A. Function return type does not match because conditional types are not resolved at runtime
B. The conditional type syntax is incorrect
C. The function parameter should be typed as number
D. The if condition should check for string instead of number
Solution
Step 1: Understand conditional types vs runtime
Conditional types exist only at compile time and cannot be used to enforce runtime return types directly.
Step 2: Check function return
The function tries to return a value matching Check<T>, but TypeScript cannot guarantee this at runtime, causing a type error.
Final Answer:
Function return type does not match because conditional types are not resolved at runtime -> Option A
Quick Check:
Conditional types = compile-time only [OK]
Hint: Remember conditional types are compile-time, not runtime checks [OK]
Common Mistakes:
Thinking conditional types work at runtime
Misusing conditional type syntax
Changing parameter type unnecessarily
5. How can conditional types replace multiple overloads in this example?
function format(value: string): string;
function format(value: number): string;
function format(value: any): string {
return value.toString();
}
Choose the best conditional type signature to replace these overloads.
hard
A. function format<T>(value: T): T extends string | number ? string : never;
B. function format<T>(value: T): string;
C. function format(value: T): T extends string ? string : string;
D. function format<T>(value: T): T extends string ? string : number;
Solution
Step 1: Understand original overloads
Both string and number inputs return a string output.
Step 2: Choose conditional type matching overloads
function format<T>(value: T): T extends string | number ? string : never; restricts T to string or number and returns string for those, else never (disallowed).
Step 3: Check other options
function format<T extends string | number>(value: T): T extends string ? string : string;'s return type is redundant; function format<T>(value: T): string; allows any type; function format<T>(value: T): T extends string ? string : number; returns number incorrectly.
Final Answer:
function format<T>(value: T): T extends string | number ? string : never; -> Option A
Quick Check:
Conditional type restricts inputs and return string [OK]
Hint: Use conditional type to restrict inputs and unify return type [OK]
Common Mistakes:
Allowing any input type without restriction
Returning wrong types for some inputs
Ignoring the need to exclude unsupported types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.