typescript add generic type to function definition code example
Example 1: typescript generic function
function firstElement<Type>(arr: Type[]): Type {
return arr[0];
}
// s is of type 'string'
const s = firstElement(["a", "b", "c"]);
// n is of type 'number'
const n = firstElement([1, 2, 3]);
Example 2: typescript generic type
function identity<T>(arg: T): T {
return arg;
}Try