TypeScript Generics Quiz

TypeScript
0 Passed
0% acceptance

A 40-question TypeScript quiz exploring generic type parameters, constraints, generic functions, interfaces, classes, defaults, inference, and advanced patterns.

40 Questions
~80 minutes
1

Question 1

What is a generic type parameter?

A
A placeholder type used for reusable type definitions
B
A runtime argument
C
A special numeric literal type
D
A decorator-only feature
2

Question 2

Why are generics useful?

A
They allow flexible yet type-safe code reuse
B
They remove type checking
C
They make all types optional
D
They automatically infer enums
3

Question 3

What is T in the function below?

typescript
function wrap<T>(value: T) { return value; }
A
A type parameter
B
A tuple
C
A primitive type
D
A decorator
4

Question 4

What does the function return type become?

typescript
function first<T>(list: T[]): T {
  return list[0];
}
A
The type of the array elements
B
any
C
unknown
D
never
5

Question 5

How does TypeScript infer generic parameters?

A
From argument types
B
From variable names
C
From decorators
D
From comments
6

Question 6

What is inferred when calling id(42)?

typescript
function id<T>(x: T) { return x }
const v = id(42);
A
T = number
B
T = any
C
T = string
D
T = boolean
7

Question 7

Generic functions allow calling code to:

A
Specify or infer the concrete types to use
B
Remove all type constraints
C
Skip static analysis entirely
D
Define new keywords
8

Question 8

What does a generic interface define?

A
A reusable structure with a type parameter
B
A concrete numeric object
C
A runtime function
D
A built-in class
9

Question 9

What is Box<string>?

typescript
interface Box<T> { value: T }
const b: Box<string> = { value: 'ok' }
A
An interface where T is replaced with string
B
A union type
C
A tuple of strings
D
A mapped type
10

Question 10

Why use generic type aliases?

A
To create reusable patterns independent of specific types
B
To remove type parameters entirely
C
To enforce runtime constraints
D
To declare enums
11

Question 11

What do generic classes allow?

A
Operations on type-parameterized instances
B
Runtime type substitution
C
Automatic narrowing
D
Tuple enforcement
12

Question 12

What is the type parameter of Store<number>?

typescript
class Store<T> {
  constructor(public item: T) {}
}
const s = new Store<number>(5);
A
number
B
string
C
boolean
D
never
13

Question 13

What does T extends U mean?

A
T must be assignable to U
B
T becomes any
C
T must be a primitive
D
T is always wider than U
14

Question 14

Which types are allowed for T?

typescript
function logLength<T extends { length: number }>(v: T) {
  return v.length;
}
A
Any type with a length property
B
number only
C
boolean only
D
never
15

Question 15

Why use constraints with generics?

A
To ensure generics are used only with compatible structures
B
To prevent inference
C
To require enums only
D
To disable interfaces
16

Question 16

What do default generic types allow?

A
Providing fallback type arguments when none are supplied
B
Creating enums
C
Skipping inference
D
Preventing specialization
17

Question 17

What type is returned when no T is provided?

typescript
interface Resp<T = string> { data: T }
const r: Resp = { data: 'ok' };
A
string
B
unknown
C
never
D
boolean
18

Question 18

Defaults are commonly used for:

A
Optional configuration types
B
Runtime validation
C
Decorators
D
Primitive conversion
19

Question 19

What does keyof T represent?

A
The union of property names of T
B
The instance of T
C
A tuple of keys
D
The type of constructor
20

Question 20

What does pick do?

typescript
function pick<T, K extends keyof T>(obj: T, key: K) {
  return obj[key];
}
A
Returns a property value of obj using a key constrained by T
B
Creates a tuple
C
Converts keys to numbers
D
Creates a class instance
21

Question 21

Which describes a generic utility type?

A
A reusable template for transforming types
B
A runtime object
C
A numeric wrapper
D
A decorator factory
22

Question 22

Which built-in type uses generics?

A
Promise<T>
B
Date
C
RegExp
D
Symbol
23

Question 23

Why avoid using any for generic parameters?

A
It removes relationships between input and output types
B
It causes syntax errors
C
It prevents inference
D
It disables interfaces
24

Question 24

What is the inferred type of out?

typescript
function toArr<T>(v: T) { return [v]; }
const out = toArr('x');
A
string[]
B
any[]
C
number[]
D
unknown[]
25

Question 25

Generic inference works best when:

A
Arguments provide clear structural type information
B
Arguments are always any
C
No parameters are used
D
The function returns void
26

Question 26

What does the map function produce?

typescript
function map<T>(arr: T[], fn: (v: T) => T): T[] {
  return arr.map(fn);
}
A
An array of T
B
A tuple
C
A union type
D
unknown
27

Question 27

Which describes partial application of generics?

A
Supplying some type parameters while inferring the rest
B
Defining nested enums
C
Creating runtime merges
D
Extending primitive types
28

Question 28

What keys can k accept?

typescript
function get<T, K extends keyof T>(obj: T, k: K) {
  return obj[k];
}
A
Any key present on obj
B
Only string
C
Only number
D
Only boolean
29

Question 29

Constraints on generics prevent:

A
Passing incompatible types
B
Using functions
C
Declaring classes
D
Inferencing
30

Question 30

What requirement must U meet?

typescript
function pair<T, U extends T>(a: T, b: U) {
  return [a, b];
}
A
U must be a subtype of T
B
U must be any
C
U must be number
D
U must be primitive only
31

Question 31

Generics help express relationships between:

A
Input and output types
B
Runtime values
C
Module names
D
File paths
32

Question 32

Generic composition helps with:

A
Building reusable data structures
B
Creating decorators
C
Runtime compression
D
Strict tuple enforcement only
33

Question 33

Why do many TS utility types rely on generics?

A
They must transform arbitrary types safely
B
They manipulate runtime data
C
They require DOM access
D
They restrict all types
34

Question 34

What advantage do generic return types offer?

A
Outputs match input structure precisely
B
Outputs are always primitive
C
Outputs ignore narrowing
D
Outputs lose type information
35

Question 35

Generic libraries are safer because:

A
They maintain type relationships across data transformations
B
They turn all types into primitives
C
They skip inference
D
They use only strings
36

Question 36

Complex generics often involve:

A
Multiple type parameters and constraints
B
Runtime patching
C
Automatic inference failures
D
Literal-only types
37

Question 37

Generic constraints allow creating:

A
Specialized behavior based on property requirements
B
New primitive types
C
Runtime exceptions
D
Automatic narrowing
38

Question 38

Generic inference reduces need for:

A
Manual type annotations
B
Constraints
C
Interfaces
D
Return statements
39

Question 39

What does a generic data wrapper provide?

A
A reusable container for various types
B
Runtime polymorphism
C
Automatic serialization
D
Dynamic imports
40

Question 40

Generics ultimately help developers by:

A
Balancing flexibility and safety in reusable code
B
Allowing only primitive types
C
Turning off static analysis
D
Forcing explicit annotations everywhere

QUIZZES IN TypeScript