Set the return type of an arrow function in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
3 min

banner

# Set the return type of an arrow function in TypeScript

You can set the return type of an arrow function in TypeScript right after its parameters.

Once a function's return type is set, the type checker alerts us if the function returns a value of a different type.

index.ts
// ๐Ÿ‘‡๏ธ with arrow function const greet = (name: string): string => { return `Hello ${name}`; }; console.log(greet('Bobby Hadz')); // ๐Ÿ‘‰๏ธ "Hello Bobby Hadz"

set return type of arrow function

The code for this article is available on GitHub

Here is an example that sets the return type of an arrow function in a class.

index.ts
class Employee { constructor(public name: string) { this.name = name; } // ๐Ÿ‘‡๏ธ with class method greet = (): string => { return `Hello ${this.name}`; }; } const employee = new Employee('Bobby Hadz'); console.log(employee.greet()); // ๐Ÿ‘‰๏ธ "Hello Bobby Hadz"

set return type of arrow function in class

The first example shows how to specify the return type of an arrow function.

The greet function takes a single parameter of type string and returns a value of type string.

Now that we've set the function's return type, the type checker will throw an error if we try to return a value of a different type or forget to return a value from the function.

index.ts
// โ›”๏ธ A function whose declared type is neither // 'void' nor 'any' must return a value.ts(2355) const greet = (name: string): string => { // return `Hello ${name}`; };

The issue in the example is that the function doesn't return anything, but has a return type of string.

Explicitly typing a function is useful because it allows us to leverage the type checker.

# TypeScript can sometimes infer the return type

Had we not set the function's return type, TypeScript would have tried to infer it.

index.ts
// ๐Ÿ‘‡๏ธ const greet: (name: string) => string const greet = (name: string) => { return `Hello ${name}`; };

typescript inferring-return-type

The code for this article is available on GitHub

TypeScript is able to infer the function's return type in this case. However, it wouldn't alert us if we forgot to return a string because we haven't set the function's return type.

index.ts
// ๐Ÿ‘‡๏ธ no errors const greet = (name: string) => { // return `Hello ${name}`; };

# Using a type alias or an interface

If your function definition gets busy, extract the return type into a type alias or an interface.

index.ts
type Person = { name: string; salary: number; department: string; }; const getEmployee = (): Person => { return { name: 'Bobby Hadz', salary: 100, department: 'development', }; };

Type aliases and interfaces are useful when your function's definition gets busy or you have to reuse a type in multiple places.

# Using an implicit arrow function return

The syntax is the same when using an implicit arrow function return.

index.ts
type Person = { name: string; salary: number; department: string; }; // ๐Ÿ‘‡๏ธ implicit arrow function return const getEmployee = (): Person => ({ name: 'Bobby Hadz', salary: 100, department: 'development', });
The code for this article is available on GitHub

# Arrow function returning one of multiple types

If your arrow function might return values of different types, use a union type.

index.ts
function getStringOrNumber(): string | number { if (Math.random() > 0.5) { return 100; } return 'bobbyhadz.com'; }

The function in the example returns a value of type string or number.

I've also written an article on how to define an array with multiple types.

# Setting the return type of an arrow function in a class

If you define a class method using an arrow function, you can set the arrow function's return type right after its parameters.

index.ts
class Employee { constructor(public name: string) { this.name = name; } // ๐Ÿ‘‡๏ธ string return type greet = (): string => { return `Hello ${this.name}`; }; } const employee = new Employee('Bobby Hadz'); console.log(employee.greet()); // ๐Ÿ‘‰๏ธ "Hello Bobby Hadz"
The code for this article is available on GitHub

The greet method has a return type of string, so trying to return a value of a different type would cause an error.

If you need to use generics in arrow functions, click on the link and follow the instructions.

If you need to pass a function as a parameter, check out the following article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev