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?
This 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?
AT must always be equal to U.
BIf T is assignable to U, use type X; otherwise, use type Y.
CT is a subtype of X or Y.
DT is a function returning X or Y.
✗ 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?
Afunction foo<T extends string | number>() {}
Bfunction foo<T = string | number>() {}
Cfunction foo<T>() where T is string or number {}
Dfunction foo<T: string | 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)?
AType error: boolean does not satisfy constraint.
BRuntime error.
CNo error, boolean is allowed.
DWarning only, code compiles.
✗ Incorrect
Boolean is not assignable to string or number, so TypeScript shows a type error.
What is the purpose of generic conditional constraints?
ATo disable type checking.
BTo make all types equal.
CTo create types that change based on input types.
DTo convert types to strings.
✗ Incorrect
They allow types to adapt depending on conditions, improving flexibility and safety.
Which of these is a valid conditional type?
Atype Result<T> = T == number ? string : boolean;
Btype Result<T> = if T is number then string else boolean;
Ctype Result<T> = T ? string : boolean;
Dtype Result<T> = T extends number ? string : boolean;
✗ 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.
Practice
(1/5)
1.
What does a generic conditional constraint in TypeScript allow you to do?
For example, in function example(arg: T) {}, what is the main purpose?
easy
A. Control the generic type based on a condition to improve type safety
B. Make the function run faster by optimizing loops
C. Automatically convert types to strings
D. Disable type checking for the generic parameter
The conditional type must be used in the argument type, not in the generic constraint part.
Step 2: Check each option
function test<T>(arg: T extends string ? number : boolean) {} correctly places the conditional type in the argument type: arg: T extends string ? number : boolean.
Final Answer:
function test<T>(arg: T extends string ? number : boolean) {} -> Option A
Quick Check:
Conditional type goes in argument type, not generic constraint [OK]
Hint: Conditional types appear in argument or return types, not in generic constraints [OK]
Common Mistakes:
Putting conditional directly in generic constraint
Using invalid syntax like 'where'
Misplacing parentheses in conditional
3.
What is the output of this TypeScript code?
type Result<T> = T extends number ? "Number" : "Other";
const a: Result<42> = "Number";
const b: Result<string> = "Other";
console.log(a, b);
medium
A. 42 string
B. Number Other
C. Number Number
D. Other Other
Solution
Step 1: Understand the conditional type Result<T>
If T is a number, Result<T> is "Number", else "Other".
Step 2: Apply to given values
Result<42> is "Number" because 42 is a number. Result<string> is "Other" because string is not a number.
Final Answer:
Number Other -> Option B
Quick Check:
Conditional type matches number = "Number", else "Other" [OK]
Hint: Check if type extends number to pick output string [OK]
Common Mistakes:
Confusing value 42 with type number
Assuming string extends number
Mixing up output strings
4.
Identify the error in this TypeScript code using generic conditional constraints:
function checkType<T>(value: T): T extends string ? number : boolean {
if (typeof value === "string") {
return value.length;
} else {
return true;
}
}
medium
A. Function lacks explicit return type annotation
B. The function parameter should be a union type
C. The generic constraint is missing
D. Cannot return different types conditionally without type assertion
Solution
Step 1: Analyze return type conditionality
The return type depends on T, but at runtime TypeScript cannot guarantee which type is returned.
Step 2: Understand why error occurs
Returning value.length (number) or true (boolean) causes a type error because TypeScript can't confirm the return matches the conditional type without assertions.
Final Answer:
Cannot return different types conditionally without type assertion -> Option D
Quick Check:
Conditional return types need type assertions or overloads [OK]
Hint: Conditional return types need type assertions to avoid errors [OK]