0
0
Typescriptprogramming~5 mins

Generic conditional constraints in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 : Y
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.
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>() {}
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.
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.
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;
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.