Function Type in TypeScript

Last updated on 19 Jan, 2023

We can mark a variable to store only functions using Function type in TypeScript.

Setting Function Type

Here is how we set Function type:

let a: Function;

Here is a valid assignment:

a = function () {};

Here is an invalid assignment:

a = 10;

Now TypeScript compiler says:

Type 'number' is not assignable to type 'Function'.

Function Type with more specifics

Here is a Function type that accepts two numbers and return a number.

let a: (num1: number, num2: number) => number;

Now a can accept only a function that has two number parameters and that returns a number type. No other function formats are accepted.

--- ○ ---
Joby Joseph
Web Architect