Get the return Type of a Function in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
2 min

banner

# Get the return Type of a Function in TypeScript

Use the ReturnType utility type to get the return type of a function in TypeScript.

The ReturnType utility type constructs a type that consists of the return type of the provided function type.

index.ts
function sum(a: number, b: number): number { return a + b; } // ๐Ÿ‘‡๏ธ type SumReturnType = number type SumReturnType = ReturnType<typeof sum>; // โœ… From a function's type type AFunctionType = (a: number, b: string) => string; // ๐Ÿ‘‡๏ธ type AFunctionReturnType = string type AFunctionReturnType = ReturnType<AFunctionType>;

get return type of function in typescript

The code for this article is available on GitHub

We used the ReturnType utility type to get the return type of a function.

When trying to get the return type of a function from a function declaration, you have to use the typeof operator.

index.ts
function sum(a: number, b: number): number { return a + b; } // ๐Ÿ‘‡๏ธ type SumReturnType = number type SumReturnType = ReturnType<typeof sum>;

This is because ReturnType takes a type and not a function.

On the other hand, if you need to get the return type from a function's type, pass it directly to ReturnType.

index.ts
// โœ… From a function's type type AFunctionType = (a: number, b: string) => string; // ๐Ÿ‘‡๏ธ type AFunctionReturnType = string type AFunctionReturnType = ReturnType<AFunctionType>;

Notice that we didn't use the typeof operator because we are passing a type to the ReturnType utility type.

# Functions that don't return a value return void

If the function you're working with doesn't return a value, its return type will be void.

index.ts
function myFunction(a: number, b: number): void { console.log(a); console.log(b); } // ๐Ÿ‘‡๏ธ type T = void type T = ReturnType<typeof myFunction>;

functions that dont return value return void

The code for this article is available on GitHub

# Functions that might return multiple types return a union type

If the function might return values of multiple types, its return type will be a union type.

index.ts
function myFunction(a: number, b: number): string | number { return Math.random() > 0.5 ? a + b : 'bobbyhadz.com'; } // ๐Ÿ‘‡๏ธ type T = string | number type T = ReturnType<typeof myFunction>;

returning union type from function

# Functions that throw an error return the type never

If the function throws an error, its return type will be never.

index.ts
function myFunction(): never { throw new Error('Something went wrong'); } // ๐Ÿ‘‡๏ธ type T = never type T = ReturnType<typeof myFunction>;
The code for this article is available on GitHub

# 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