Generics in TypeScript Quiz

This quiz explores Generics in TypeScript, including topics such as generic functions, generic classes, constraints in generics, and built-in generic types. It aims to provide a comprehensive understanding of how generics enhance code reusability, flexibility, and type safety in TypeScript.

Last Updated :
Discuss
Comments

Question 1

What is the primary purpose of generics in TypeScript?

  • To create dynamic functions without type checking.

  • To enforce strict types on all variables.

  • To allow type flexibility while maintaining type safety.

  • To remove the need for interfaces and type aliases.

Question 2

Which of the following is a correct example of a generic function?

TypeScript
function identity<T>(value: T): T {
    return value;
}


  • The function uses a generic type <T> for flexibility.

  • <T> is unnecessary and makes the function invalid.

  • The identity function is not generic.

  • The identity function must define a fixed type for T.

Question 3

What is the output type of the following generic function?

JavaScript
function wrap<T>(value: T): { value: T } {
    return { value };
}


  • T

  • { value: T }

  • void

  • unknown

Question 4

Which of the following defines a generic class?

TypeScript
class Box<T> {
    private content: T;
    constructor(content: T) {
        this.content = content;
    }
    getContent(): T {
        return this.content;
    }
}


  • Box is a generic class where T can be replaced with a specific type.

  • T must always be a string in this class.

  • T is a built-in generic type.

  • The class is invalid because it uses <T>.

Question 5

What is the purpose of constraints in generics?

  • To limit the use of generics to specific classes only.

  • To enforce that a type parameter meets certain requirements

  • To ensure that generics cannot be used in functions.

  • To restrict the number of type parameters in a function.

Question 6

Which of the following constrains a generic type to objects with a length property?

TypeScript
function logLength<T extends { length: number }>(arg: T): void {
    console.log(arg.length);
}


  • The generic type T must be a number.

  • The generic type T must be a string.

  • The generic type T must have a length property.

  • The function is invalid because T cannot have constraints.

Question 7

What is the purpose of the Partial<T> built-in generic type in TypeScript?

  • To make all properties of T required.

  • To enforce strict null checking on T.

  • To make all properties of T optional.

  • To exclude properties from T.

Question 8

How is the Record

generic type used in TypeScript?

  • To define an object type with keys of type K and values of type V.

  • To merge two types K and V.

  • To restrict the properties of K to a single type.

  • To make K optional in all cases.

Question 9

What is the key difference between generics and type aliases?

  • Generics enforce strict types, while type aliases do not.

  • Type aliases are more flexible than generics.

  • Generics allow type flexibility, whereas type aliases define specific structures.

  • Generics are only used for arrays, while type aliases are used for objects.

Question 10

Which of the following is a correct use of the ReadonlyArray<T> built-in generic type?

TypeScript
function printNumbers(numbers: ReadonlyArray<number>): void {
    numbers.forEach((num) => console.log(num));
    // numbers.push(10); // Error
}


  • It ensures the array numbers cannot be modified.

  • It allows adding new elements to numbers.

  • It is identical to a regular array.

  • It throws an error at runtime if numbers is modified.

There are 10 questions to complete.

Take a part in the ongoing discussion