Generics Questions with Answers

18 practice questions on Generics (TypeScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

Given interface Box<T> { value: T }, what is the type of b.value when b: Box<number>?

  1. unknown
  2. number
  3. T
  4. Box<number>

Answer: B. number

Explanation

Supplying number for T replaces every T in the interface, so value becomes number.

Step-by-step solution

  1. Box(T) has a value property of type T.
  2. Box(number) replaces every T with number.
  3. So b.value has type number.
  4. The answer is option B.

Why the other options are wrong

  • A. Unknown would apply only if no type argument were given.
  • C. T is a placeholder; with Box<number> it becomes number.
  • D. b.value is the property, not the whole Box type.

Tip: A generic type argument replaces every T in the type's members.

Question 2Easy

Which type is identical to number[]?

  1. [number]
  2. Array<number[]>
  3. ReadonlyArray<number>
  4. Array<number>

Answer: D. Array<number>

Explanation

T[] is shorthand for the generic Array<T>. [number] is a one-element tuple and ReadonlyArray lacks mutating methods.

Step-by-step solution

  1. number[] is shorthand for Array(number).
  2. [number] is a one-element tuple, which is different.
  3. ReadonlyArray(number) lacks the mutating methods, so it is different.
  4. So the identical type is option D, Array(number).

Why the other options are wrong

  • A. [number] is a tuple of length one, not an array of numbers.
  • B. Array<number[]> is an array of arrays, not an array of numbers.
  • C. ReadonlyArray<number> is read-only, so it is not identical to number[].

Tip: T[] and Array<T> are the same type; tuples and read-only arrays are not.

Question 3Easy

In function identity<T>(x: T): T { return x; }, what is T?

  1. The name of a built-in TypeScript class
  2. A runtime argument that holds the value's type name
  3. A type parameter that is filled in for each call
  4. A shorthand that means the same as any

Answer: C. A type parameter that is filled in for each call

Explanation

T is a placeholder for a type, usually inferred from the argument. It links the parameter type to the return type.

Step-by-step solution

  1. In identity(x: T): T, T is a type parameter.
  2. It is a placeholder for a type that is filled in each time the function is called.
  3. TypeScript usually infers T from the argument, and the return type follows it.
  4. So the answer is option C.

Why the other options are wrong

  • A. T is not a built-in class; it is a placeholder name.
  • B. T is a type, not a runtime argument that holds a type name.
  • D. T is not a shorthand for any; it is inferred per call and keeps types precise.

Tip: A type parameter like T links the input type to the output type across the function.

Question 4Easy

Why prefer a generic function like first<T>(arr: T[]): T over first(arr: any[]): any?

  1. Generic functions run faster after compilation
  2. Generics add runtime type checks to the function
  3. any is not allowed when strict mode is on
  4. It keeps the input and output types linked

Answer: D. It keeps the input and output types linked

Explanation

With any, the return value loses all type information. With T, calling first on a string[] returns a string, and misuse is caught.

Step-by-step solution

  1. first(T)(arr: T[]): T uses T to link input and output types.
  2. For a string[] input, the return is typed as string.
  3. With any, the return type is any, so type information is lost and errors go unnoticed.
  4. So the answer is option D, the types stay linked.

Why the other options are wrong

  • A. Generics are erased at compile time and do not change runtime speed.
  • B. Generics add no runtime checks; they are compile-time only.
  • C. Any is allowed under strict mode; the problem is the lost type information.

Tip: Prefer generics over any to keep input and output types connected.

Question 5Medium

What is the type of m.b?

function merge<T extends object, U extends object>(a: T, b: U): T & U {
  return { ...a, ...b };
}
const m = merge({ a: 1 }, { b: "x" });
const check: string = m.b;
  1. unknown
  2. number
  3. string | number
  4. string

Answer: D. string

Explanation

The result type is the intersection { a: number } & { b: string }, so m.b is a string.

Step-by-step solution

  1. Merge returns T & U, an intersection of the two argument types.
  2. A is { a: number } and b is { b: string }.
  3. So m is { a: number } & { b: string }, and m.b is string.
  4. The answer is option D.

Why the other options are wrong

  • A. Unknown would apply only if the type were not inferred; here b is typed.
  • B. Number is the type of a.a, not of b.
  • C. The b value is the string 'x', so only string applies.

Tip: An intersection T & U has every property from both types.

Question 6Medium

Which call to len fails to compile?

function len<T extends { length: number }>(x: T) { return x.length; }
len([1, 2]);
len("ab");
len({ length: 3 });
len(10);
  1. len({ length: 3 })
  2. len("ab")
  3. len(10)
  4. len([1, 2])

Answer: C. len(10)

Explanation

The constraint requires a numeric length property. Arrays, strings and the object literal all have one; a number does not.

Step-by-step solution

  1. The constraint T extends { length: number } needs a numeric length property.
  2. An array has length, and a string has length, and the object literal has length 3.
  3. A number has no length property, so len(10) does not satisfy the constraint.
  4. So the call that fails is len(10), the answer is option C.

Why the other options are wrong

  • A. The object literal has a numeric length, so this call compiles.
  • B. A string has a length property, so this call compiles.
  • D. An array has a length property, so this call compiles.

Tip: A generic constraint checks that the argument has the required members.

Question 7Medium

What happens on the last line?

function getProp<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}
getProp({ a: 1 }, "b");
  1. Compile error: "b" is not assignable to "a"
  2. Runtime error: property b not found
  3. It compiles, and K is inferred as plain string
  4. It compiles and returns undefined

Answer: A. Compile error: "b" is not assignable to "a"

Explanation

K extends keyof T restricts key to the actual property names of obj. keyof { a: number } is "a".

Step-by-step solution

  1. K extends keyof T limits key to the property names of obj.
  2. Obj is { a: number }, so keyof T is 'a'.
  3. Passing 'b' is not assignable to 'a', so TypeScript reports an error.
  4. The answer is option A, a compile error.

Why the other options are wrong

  • B. The problem is caught at compile time, so there is no runtime error.
  • C. K is not inferred as string; it must be one of the keys of obj.
  • D. The call does not compile, so no return value is produced.

Tip: K extends keyof T makes the compiler reject key names that are not on the object.

Question 8Medium

What is the type of r?

function map<T, U>(arr: T[], fn: (x: T) => U): U[] {
  return arr.map(fn);
}
const r = map([1, 2], n => n.toString());
const check: string[] = r;
  1. (string | number)[]
  2. unknown[]
  3. string[]
  4. number[]

Answer: C. string[]

Explanation

T is inferred as number from the array and U as string from the callback's return value, so the result is U[] = string[].

Step-by-step solution

  1. T is inferred as number from the array [1, 2].
  2. U is inferred from the callback's return value, which is toString(), a string.
  3. So the return type is U[] with U = string, giving string[].
  4. The answer is option C.

Why the other options are wrong

  • A. The result is not a union of string and number; U comes from the callback return only.
  • B. unknown[] would need U to be unknown, but the callback returns a string.
  • D. The elements are strings, not numbers, after toString().

Tip: Generic return types follow from the callback's return type, so check what the callback returns.

Question 9Hard

What does TypeScript report for this class?

class Store<T> {
  static fallback: T;
  items: T[] = [];
}
  1. Error: statics cannot reference class type parameters
  2. No error; every Store<T> gets its own fallback
  3. No error; fallback is typed as unknown
  4. Error: items must be initialised in a constructor

Answer: A. Error: statics cannot reference class type parameters

Explanation

A static member belongs to the class itself and is shared by every Store<T>, so a single static cannot depend on T.

Step-by-step solution

  1. A static member belongs to the class itself, not to each instance.
  2. It is shared by every Store(T), whatever T is.
  3. So a static member cannot refer to the class type parameter T, and TypeScript reports an error.
  4. The answer is option A.

Why the other options are wrong

  • B. There is one shared static, not one per Store<T>, and the error stops the declaration.
  • C. Fallback is not typed as unknown; the declaration is rejected.
  • D. Items is initialised inline, so no constructor is needed for it.

Tip: Statics cannot use class type parameters; put such members on instances instead.

Question 10Hard

Which line fails to compile?

let ro: readonly number[] = [1, 2];
let mut: number[] = [3, 4];
ro = mut;   // line X
mut = ro;   // line Y
  1. Only line Y
  2. Both lines
  3. Only line X
  4. Neither line

Answer: A. Only line Y

Explanation

A mutable array can be used where a read-only one is expected. The reverse is unsafe because the mutable type would allow push on data promised to be read-only.

Step-by-step solution

  1. Line X assigns a mutable number[] to a readonly number[] variable.
  2. That is safe, because mutable arrays can be read as read-only.
  3. Line Y assigns a readonly number[] to a mutable number[] variable.
  4. That is unsafe, since the mutable type allows push, so Line Y fails and the answer is option A.

Why the other options are wrong

  • B. Line X is allowed, so it does not fail.
  • C. Line X compiles; it is line Y that is the error.
  • D. Line Y fails, so the answer is not neither.

Tip: Mutable to read-only is safe; read-only to mutable is an error.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all TypeScript topics · All topics