MCQs on Advanced Generics | TypeScript

Mastering advanced generics in TypeScript allows developers to create more flexible, reusable, and type-safe code. This guide delves into recursive generics, variadic tuple types, conditional types, and advanced type constraints.


Advanced Generics in TypeScript – 30 Multiple Choice Questions

1. Recursive Generics

  1. What is a recursive generic in TypeScript?
    • A) A type that references itself within its definition
    • B) A type that can only be used once
    • C) A generic type that doesn’t allow inheritance
    • D) A type that performs type checks recursively
  2. How can you define a recursive generic in TypeScript?
    • A) By using a generic that refers to itself in its definition
    • B) By using the any type
    • C) By using an array of types
    • D) By defining a function that returns itself
  3. In recursive generics, which is typically used as the base case for the recursion?
    • A) null
    • B) void
    • C) A predefined type
    • D) A constant value
  4. What is the primary benefit of using recursive generics?
    • A) They make the code more concise
    • B) They allow for flexible data structures like trees or linked lists
    • C) They improve runtime performance
    • D) They increase the complexity of type checking
  5. Which of the following is a correct example of a recursive generic type in TypeScript?
    • A) type LinkedList<T> = T | LinkedList<T>
    • B) type LinkedList<T> = T[] | T
    • C) type LinkedList<T> = { value: T } | { next: LinkedList<T> }
    • D) type LinkedList<T> = T | T[]

2. Variadic Tuple Types

  1. What are variadic tuple types in TypeScript?
    • A) Types that allow an array of a fixed number of elements
    • B) Types that represent tuples with a flexible number of elements
    • C) Types that can only contain strings
    • D) Types that hold a fixed set of objects
  2. How do variadic tuple types allow flexibility in TypeScript?
    • A) They enable functions to accept a variable number of arguments
    • B) They define tuples with predefined length
    • C) They restrict the tuple elements to a single data type
    • D) They allow a tuple to store multiple objects of different types
  3. What is the correct syntax for defining a variadic tuple type in TypeScript?
    • A) type Tuple<T> = [T, ...T[]];
    • B) type Tuple<T> = [...T[]];
    • C) type Tuple<T> = [T, T];
    • D) type Tuple<T> = [T, T, T];
  4. How do variadic tuple types benefit TypeScript’s type inference?
    • A) By allowing TypeScript to infer the types of arguments passed to a function
    • B) By restricting the types of tuples to a fixed structure
    • C) By making the tuple type completely dynamic
    • D) By limiting the use of generics
  5. In which scenario would you typically use a variadic tuple type?
  • A) When you need a tuple that can store a sequence of types
  • B) When you want to store a fixed number of strings
  • C) When defining a tuple with different structures
  • D) When you want a tuple with a single numeric value

3. Conditional Types in Generics

  1. What are conditional types in TypeScript?
  • A) Types that depend on the value of a given type
  • B) Types that are used only within specific conditions
  • C) Types that change based on runtime conditions
  • D) Types that support type inference automatically
  1. Which of the following is the syntax for conditional types in TypeScript?
  • A) T extends U ? X : Y
  • B) T ? X : Y
  • C) T ? Y : Z : X
  • D) T -> X ? Y : Z
  1. What is the result of a conditional type when the condition evaluates to true?
  • A) The first type (X) is used
  • B) The second type (Y) is used
  • C) TypeScript throws a compile-time error
  • D) The condition is ignored
  1. What does the following conditional type represent: type IsString<T> = T extends string ? "Yes" : "No";?
  • A) If T is a string, the type is "Yes", otherwise "No"
  • B) It checks if T is a string and returns a boolean
  • C) It checks if T is a string and returns the string type itself
  • D) It checks if T is a string and returns undefined
  1. How can conditional types help with type safety in TypeScript?
  • A) By enforcing the correct type at compile time
  • B) By automatically casting types at runtime
  • C) By making types completely dynamic
  • D) By ignoring types during the type checking process
  1. Can you combine multiple conditional types in TypeScript?
  • A) Yes, by using nested conditional types
  • B) No, only one conditional type can be used
  • C) Yes, but only for simple cases
  • D) No, conditional types are exclusive
  1. How does TypeScript infer the type when using conditional types?
  • A) It evaluates the condition at runtime
  • B) It checks the type during compile time
  • C) It evaluates types based on specific runtime conditions
  • D) It doesn’t infer types automatically
  1. In a conditional type, what happens when the condition is not satisfied?
  • A) TypeScript uses the default type
  • B) TypeScript throws an error
  • C) The second type (Y) is used
  • D) The first type (X) is used

4. Advanced Type Constraints

  1. What is the purpose of type constraints in TypeScript?
  • A) To limit the types that can be assigned to a generic
  • B) To allow any type to be assigned to a generic
  • C) To make generics type-safe
  • D) To automatically infer types for generics
  1. What is the syntax for adding a constraint to a generic in TypeScript?
  • A) <T extends Constraint>
  • B) <T: Constraint>
  • C) <T as Constraint>
  • D) <T with Constraint>
  1. Which of the following defines a constrained generic that only accepts types with a name property?
  • A) type MyType<T extends { name: string }> = T;
  • B) type MyType<T: { name: string }> = T;
  • C) type MyType<T extends { name: any }> = T;
  • D) type MyType<T with { name: string }> = T;
  1. What does T extends keyof SomeType mean in TypeScript?
  • A) It restricts T to a key of SomeType
  • B) It checks if T is a valid type of SomeType
  • C) It allows T to be any type within SomeType
  • D) It makes T a property of SomeType
  1. What is the benefit of using extends with generics in TypeScript?
  • A) It makes the type more flexible and reusable
  • B) It enforces type constraints for better type safety
  • C) It automatically converts types at runtime
  • D) It ignores the type checking process
  1. How do you specify a generic type constraint with multiple conditions?
  • A) T extends A & B
  • B) T extends A | B
  • C) T extends (A, B)
  • D) T extends [A, B]
  1. What happens when a constraint is not satisfied for a generic type?
  • A) TypeScript will throw an error
  • B) TypeScript will automatically choose another type
  • C) TypeScript will treat the type as any
  • D) TypeScript will convert the type to undefined
  1. How can you restrict a generic to only accept numbers in TypeScript?
  • A) T extends number
  • B) T as number
  • C) T is number
  • D) T type number
  1. Can you use extends with primitive types in TypeScript generics?
  • A) Yes, to restrict types to a specific primitive
  • B) No, only complex types can be used
  • C) Yes, but only for arrays
  • D) No, extends does not work with primitives
  1. How can you constrain a generic to accept only classes with a constructor in TypeScript?
  • A) T extends { new(): any }
  • B) T extends { constructor: any }
  • C) T extends Function
  • D) T extends Class
  1. What is the effect of using extends with union types in TypeScript?
  • A) It allows a generic to accept multiple types
  • B) It restricts a generic to only one type in the union
  • C) It prevents type checking
  • D) It forces the type to be dynamic
  1. How would you constrain a generic type to be a subtype of an interface with optional properties?
  • A) T extends { name?: string }
  • B) T extends { name: string? }
  • C) T extends { name }
  • D) T extends string | number

Answer Key

QnoAnswer
1A) A type that references itself within its definition
2A) By using a generic that refers to itself in its definition
3C) A predefined type
4B) They allow for flexible data structures like trees or linked lists
5A) `type LinkedList<T> = T
6B) Types that represent tuples with a flexible number of elements
7A) They enable functions to accept a variable number of arguments
8A) type Tuple<T> = [T, ...T[]];
9A) By allowing TypeScript to infer the types of arguments passed to a function
10A) When you need a tuple that can store a sequence of types
11A) Types that depend on the value of a given type
12A) T extends U ? X : Y
13A) The first type (X) is used
14A) If T is a string, the type is "Yes", otherwise "No"
15A) By enforcing the correct type at compile time
16A) Yes, by using nested conditional types
17B) It checks the type during compile time
18C) The second type (Y) is used
19A) To limit the types that can be assigned to a generic
20A) <T extends Constraint>
21A) type MyType<T extends { name: string }> = T;
22A) T extends keyof SomeType
23B) It enforces type constraints for better type safety
24A) T extends A & B
25A) TypeScript will throw an error
26A) T extends number
27A) Yes, to restrict types to a specific primitive
28A) T extends { new(): any }
29A) It allows a generic to accept multiple types
30A) T extends { name?: string }

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top