Generic ( <T> )
Try it yourself →Type-independent, reusable structure.
Syntax
function name<T>(x: T): T { }Description
T is a 'type placeholder'; the actual type is determined when calling the function. It provides flexibility while maintaining type safety.
Example
function ilk<T>(arr: T[]): T {
return arr[0];
}
console.log(ilk([10, 20, 30]));
console.log(ilk(["a", "b"]));Expected output
10 a
When to use?
Helpers working with any type (first element, box, items).