Generic Function in TypeScript
In the previous lessons, we made an identity function that took parameters of a generic type. Now we will learn how to describe the type of such a function and create generic interfaces.
A function of a generic type can also be written as an arrow function. Let's declare a variable func, which will have the type of a generic function:
function myFunc <T> (data: T): T {
return data;
}
let func: <T> (data: T) => T = myFunc;
You can also specify another symbol for the generic type of such a variable. The main thing is to ensure that the type names do not conflict:
function myFunc <T> (data: T): T {
return data;
}
let func: <U> (data: U) => U = myFunc;
A generic type can be written by enclosing the call signature and the function type in curly braces:
function myFunc < T > (data: T): T {
return data;
}
let func: {<U> (data: U): U} = myFunc;
Write a function to determine the length of a generic array in the variants described in this lesson.