TypeScript Generics Quiz
A 40-question TypeScript quiz exploring generic type parameters, constraints, generic functions, interfaces, classes, defaults, inference, and advanced patterns.
Question 1
What is a generic type parameter?
Question 2
Why are generics useful?
Question 3
What is T in the function below?
function wrap<T>(value: T) { return value; }Question 4
What does the function return type become?
function first<T>(list: T[]): T {
return list[0];
}Question 5
How does TypeScript infer generic parameters?
Question 6
What is inferred when calling id(42)?
function id<T>(x: T) { return x }
const v = id(42);Question 7
Generic functions allow calling code to:
Question 8
What does a generic interface define?
Question 9
What is Box<string>?
interface Box<T> { value: T }
const b: Box<string> = { value: 'ok' }Question 10
Why use generic type aliases?
Question 11
What do generic classes allow?
Question 12
What is the type parameter of Store<number>?
class Store<T> {
constructor(public item: T) {}
}
const s = new Store<number>(5);Question 13
What does T extends U mean?
Question 14
Which types are allowed for T?
function logLength<T extends { length: number }>(v: T) {
return v.length;
}Question 15
Why use constraints with generics?
Question 16
What do default generic types allow?
Question 17
What type is returned when no T is provided?
interface Resp<T = string> { data: T }
const r: Resp = { data: 'ok' };Question 18
Defaults are commonly used for:
Question 19
What does keyof T represent?
Question 20
What does pick do?
function pick<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}Question 21
Which describes a generic utility type?
Question 22
Which built-in type uses generics?
Question 23
Why avoid using any for generic parameters?
Question 24
What is the inferred type of out?
function toArr<T>(v: T) { return [v]; }
const out = toArr('x');Question 25
Generic inference works best when:
Question 26
What does the map function produce?
function map<T>(arr: T[], fn: (v: T) => T): T[] {
return arr.map(fn);
}Question 27
Which describes partial application of generics?
Question 28
What keys can k accept?
function get<T, K extends keyof T>(obj: T, k: K) {
return obj[k];
}Question 29
Constraints on generics prevent:
Question 30
What requirement must U meet?
function pair<T, U extends T>(a: T, b: U) {
return [a, b];
}Question 31
Generics help express relationships between:
Question 32
Generic composition helps with:
Question 33
Why do many TS utility types rely on generics?
Question 34
What advantage do generic return types offer?
Question 35
Generic libraries are safer because:
Question 36
Complex generics often involve:
Question 37
Generic constraints allow creating:
Question 38
Generic inference reduces need for:
Question 39
What does a generic data wrapper provide?
Question 40
Generics ultimately help developers by:
