Function Type in TypeScript
JavaScript can have variables that store functions. In this case, TypeScript allows us to specify that the variable is of type "function". The type of a function is a combination of the parameter types and the return type. This combination is called the signature of the function.
To specify a function type for a variable, you need to list the parameters and their types in parentheses, and after the arrow =>
specify the return type. Let's look at an example. Let's declare a variable as containing a function:
let func: (x: number, y: number) => number;
Let's write a function of a given type into this variable:
let func: (x: number, y: number) => number = function(a: number, b: number): number {
return a + b;
};
Specify the function type for the variable:
let func = function(text: string): void {
alert(text);
};