Skip to main content

What is a generic type?

In TypeScript, generics are a way to create flexible and reusable code that can work with multiple types. Generics allow you to define a function, class, interface, or type that can accept one or more type parameters, which represent the types of the arguments, properties, or other elements of the code.

Generics allow you to write code that is generic, or not tied to a specific type, and that can work with any type or a set of types that you specify. You can use generics to create functions, classes, interfaces, or types that are more flexible and adaptable, and that can be used in a variety of contexts and scenarios.

Here is an example of using generics in TypeScript:

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

identity('hello');
identity(42);

class MyClass<T> {
value: T;

constructor(value: T) {
this.value = value;
}

getValue(): T {
return this.value;
}
}

const obj1 = new MyClass<string>('hello');
const obj2 = new MyClass<number>(42);

Generics are a powerful and versatile feature of TypeScript that allow you to create flexible and reusable code that can work with multiple types, and they can be useful for creating generic functions, classes, interfaces, or types that can be used in a variety of contexts and scenarios. It is important to use generics appropriately to ensure that the code is well-typed and easy to understand and maintain.