Categories
TypeScript Answers

How to declare return types for functions in TypeScript?

Spread the love

Sometimes, we want to declare return types for functions in TypeScript.

In this article, we’ll look at how to declare return types for functions in TypeScript.

How to declare return types for functions in TypeScript?

To declare return types for functions in TypeScript, we put it after the colon and before the function body.

For instance, we write

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

to set the return type of the sum to number with : number before the function body.

Conclusion

To declare return types for functions in TypeScript, we put it after the colon and before the function body.

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 *