What Does < T > Mean in TypeScript?

November 3, 2022 in Development | 2 mins read | Tagged:

Constructing components with well-defined and consistent APIs that are reusable is a crucial aspect of software engineering. The most flexible approach for creating large software systems involves developing components that can process not only current data but also data that may be introduced in the future.

In programming languages such as C# and Java, a fundamental technique for producing reusable components is the use of generics. Generics make it possible to create a component that can work with multiple types instead of only one. By doing so, users are free to utilize their own types when using these components.

The <T> syntax in TypeScript represents a type parameter, which allows you to define a generic type that can be used with different types.

In TypeScript, you can define a function, class, or interface that takes one or more type parameters by enclosing them in angle brackets < >. For example, consider the following function:

function reverse<T>(items: T[]): T[] {
  return items.reverse();

Here, the reverse function takes an array of items of type T, and returns a reversed array of items of the same type. The <T> syntax indicates that T is a type parameter that can be replaced with any actual type when the function is called. For instance, you could call the function with an array of numbers like this:

const numbers = [1, 2, 3, 4, 5];
const reversedNumbers = reverse(numbers);

In this case, the type parameter T would be inferred as number, and the reverse function would return an array of type number[].

Type parameters can be useful when you want to write code that is reusable with different types, without having to write separate functions or classes for each type. They allow you to write generic code that works with any type that meets certain constraints.

Back to top