Categories
TypeScript Answers

How to add return type for a function that returns another function in TypeScript?

Spread the love

To add return type for a function that returns another function in TypeScript, we can define a return type that returns a function like any other type.

For instance, we write

export const add: (a: number) => (b: number) => number = (a) => (b) => a + b;

to define the add function that takes a number argument and returns a function that takes another number argument and that returns a number.

And then we assign it a function that matches the signature and return type.

We can also substitute number for a generic type.

To do this, we write

const add: <A extends number, B extends number>(a: A) => (b: B) => number =
  (a) => (b) =>
    a + b;
``

And we substitute `number` for types `A` and `B` in the function signatures of the type definition.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *