Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do you implement a generic class with multiple type parameters
    undefined


How do you implement a generic class with multiple type parameters


Here's an example of implementing a generic class with multiple type parameters in TypeScript:

typescript
class Pair<T, U> {
  private first: T;
  private second: U;

  constructor(first: T, second: U) {
    this.first = first;
    this.second = second;
  }

  getFirst(): T {
    return this.first;
  }

  getSecond(): U {
    return this.second;
  }
}

// Using the generic Pair class
const stringAndNumberPair = new Pair<string, number>("Hello", 42);
console.log(stringAndNumberPair.getFirst()); // Output: "Hello"
console.log(stringAndNumberPair.getSecond()); // Output: 42

const booleanAndStringPair = new Pair<boolean, string>(true, "World");
console.log(booleanAndStringPair.getFirst()); // Output: true
console.log(booleanAndStringPair.getSecond()); // Output: "World"

In this example, the `Pair` class is defined as a generic class with two type parameters, `T` and `U`. The class has two private properties, `first` of type `T` and `second` of type `U`, which are initialized in the constructor.

The class also has two methods, `getFirst` and `getSecond`, which return the `first` and `second` properties, respectively, preserving their original types.

When creating instances of the `Pair` class, you can specify the types for `T` and `U` using angle brackets (`<>`). In the example, we create two instances:

1. `stringAndNumberPair`: This instance uses `string` for `T` and `number` for `U`, allowing us to store a string and a number pair.
2. `booleanAndStringPair`: This instance uses `boolean` for `T` and `string` for `U`, allowing us to store a boolean and a string pair.

By using multiple type parameters, the `Pair` class becomes more flexible and can handle different combinations of types, ensuring type safety throughout the code.

The key benefits of using multiple type parameters in a generic class are:

1. Increased flexibility: The class can work with various combinations of types, making it more reusable.
2. Type safety: The TypeScript compiler can enforce type checking for the properties and methods of the class, preventing type-related errors.
3. Reduced duplication: You can write a single generic class that can handle different types, avoiding the need for creating separate classes for each type combination.

Using multiple type parameters in a generic class is a powerful technique that allows you to create highly flexible and type-safe components in TypeScript[1][2][3][4].

Citations:
[1] https://www.scaler.com/topics/typescript/generic-classes-in-typescript/
[2] https://www.youtube.com/watch?v=g0MSer7WxVc
[3] https://www.geeksforgeeks.org/typescript-generic-classes/
[4] https://blog.logrocket.com/using-typescript-generics-create-reusable-components/
[5] https://www.digitalocean.com/community/tutorials/how-to-use-generics-in-typescript