DEV Community

Abdus Shobhan
Abdus Shobhan

Posted on

TypeScript

generic function to allow for a function that works with different data types. Here's a basic example of a generic function that takes an argument of any type and returns it:
typescript

Copy code
function identity(arg: T): T {
return arg;
}

// Example usage:
let output1 = identity("Hello, TypeScript!");
let output2 = identity(42);
let output3 = identity(true);

console.log(output1); // Output: Hello, TypeScript!
console.log(output2); // Output: 42
console.log(output3); // Output: true

Top comments (0)