Backbencher.dev

Interfaces as Function Types in TypeScript

Last updated on 7 Feb, 2023

Interfaces in TypeScript can also be used to define the type of a function.

Without interface, we can define the type of a function as below:

type sumFn = (a: number, b: number) => number;

Then later we can use the type as follows:

const sum: sumFn = (a: number, b: number) => a + b;

Now let us see how to implement sumFn type using interface.

interface sumFn {
  (a: number, b: number): number;
}

Note that there is no => syntax in the above snippet.

--- ○ ---
Joby Joseph
Web Architect