Recall & Review
beginner
What is a generic conditional constraint in TypeScript?
A generic conditional constraint lets you restrict a generic type based on a condition, using the
extends keyword combined with conditional types to enforce rules on the types that can be used.Click to reveal answer
beginner
How do you write a generic function that only accepts types extending
string or number?You write it like this:
function example<T extends string | number>(value: T) { /* ... */ } This means T can only be string or number.Click to reveal answer
intermediate
Explain this TypeScript type:
T extends U ? X : YThis is a conditional type. It means: if type
T can be assigned to type U, then the type is X, otherwise it is Y. It helps create types that change based on conditions.Click to reveal answer
beginner
What happens if you try to use a type that does not satisfy a generic conditional constraint?
TypeScript will show a type error and prevent compilation. This helps catch mistakes early by ensuring only allowed types are used where the generic constraint applies.
Click to reveal answer
intermediate
Give an example of a generic conditional constraint that returns different types based on input type.
Example:
type TypeName<T> = T extends string ? "string" : T extends number ? "number" : "other";This type returns a string literal describing the input type.
Click to reveal answer
What does
T extends U ? X : Y mean in TypeScript?✗ Incorrect
This is a conditional type that chooses X or Y based on whether T extends U.
Which syntax restricts a generic type to only string or number?
✗ Incorrect
The
extends keyword restricts the generic type to the union of string or number.What error occurs if you pass a boolean to
function foo(arg: T)?✗ Incorrect
Boolean is not assignable to string or number, so TypeScript shows a type error.
What is the purpose of generic conditional constraints?
✗ Incorrect
They allow types to adapt depending on conditions, improving flexibility and safety.
Which of these is a valid conditional type?
✗ Incorrect
Option D uses correct TypeScript syntax for conditional types.
Explain how generic conditional constraints help control types in TypeScript.
Think about how you can make types change based on conditions.
You got /5 concepts.
Write a simple generic conditional type that returns 'array' if the input type is an array, otherwise 'not array'.
Use <code>T extends any[] ? 'array' : 'not array'</code> pattern.
You got /4 concepts.