Categories
TypeScript Answers

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

Spread the love

Sometimes, we want to add return type for a function that returns another function in TypeScript.

In this article, we’ll look at how to add return type for a function that returns another function in TypeScript.

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

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.


### Conclusion

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.