Show List

Generics in TypeScript

Generics in TypeScript allow you to define a class, interface, or function that can work with multiple types, rather than a specific type. This allows for greater flexibility and reusability of code.

Here's a simple example of a generic function in TypeScript:

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

let output = identity<string>("Hello, World!");
console.log(output); // "Hello, World!"

In this example, the identity function takes an argument of type T and returns a value of the same type T. The type T is specified when the function is called, and in this case, it is specified as string. The generic type T allows the identity function to be used with any type, not just strings.

Here's another example of a generic class in TypeScript:

class GenericNumber<T> {
  zeroValue: T;
  add: (x: T, y: T) => T;
}

let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) {
  return x + y;
};

console.log(myGenericNumber.add(myGenericNumber.zeroValue, 10)); // 10

In this example, the GenericNumber class takes a type T and defines two properties, zeroValue and add. The zeroValue property is of type T, and the add property is a function that takes two arguments of type T and returns a value of type T. The type T is specified when an instance of the class is created, and in this case, it is specified as number. The generic type T allows the GenericNumber class to be used with any type, not just numbers.

Generics are a powerful feature of TypeScript that allow you to write reusable and flexible code. They can also help you catch type-related errors at compile time, rather than runtime.

Additionally, generics can also be used in interfaces in TypeScript. Here's an example of a generic interface:
interface GenericIdentityFn<T> {
  (arg: T): T;
}

let myIdentity: GenericIdentityFn<number> = function(arg) {
  return arg;
};

console.log(myIdentity(10)); // 10

In this example, the GenericIdentityFn interface defines a generic function that takes an argument of type T and returns a value of the same type T. The type T is specified when the interface is implemented, and in this case, it is specified as number.

You can also use generics with arrays in TypeScript. Here's an example:

function loggingIdentity<T>(arg: T[]): T[] {
  console.log(arg.length);
  return arg;
}

let output = loggingIdentity<string>(["Hello", "World"]);
console.log(output); // ["Hello", "World"]

In this example, the loggingIdentity function takes an array of type T as an argument and returns an array of the same type T. The type T is specified when the function is called, and in this case, it is specified as string.

In conclusion, generics are an important feature of TypeScript that allow you to create reusable and flexible code. They provide a way to write code that can work with multiple types, while ensuring that the correct types are used at compile time. By using generics, you can write code that is more maintainable, reusable, and type-safe.


    Leave a Comment


  • captcha text