⊗jstsPmFnFT 53 of 55 menu

Custom Type with Function in TypeScript

Sometimes it is more convenient to declare a separate type that will contain a description of the parameters and return value of the function:

type Func = (x: number, y: number) => number;

Then you can declare functions with that type.

Let's declare a function that adds two numbers as an example:

let func1: Func = function(a: number, b: number): number { return a + b; };

Now let's declare a function that multiplies two numbers:

let func2: Func = function(a: number, b: number): number { return a * b; };

The following type is given:

type Func = (x: number, y: number, z: number) => number;

Make a function of this type. Let the function receive three numbers as a parameter, and return the sum of these numbers as its result.

Declare a function type that takes a number and an array of numbers as parameters and returns an array of numbers as a result.

byenru