Skip to content
jshacks.dev
TypeScript

How to Get the Return Type of an Async Function

Combine ReturnType and Awaited in TypeScript 6 to get the resolved value type of an async function, and handle generics, overloads and rejected promises.

7 min read
Summarize in AI
Flat illustration of an editor window full of code, with the TypeScript, JavaScript and Node.js logos.

An async function always returns a promise. When you write async function loadUser() { return { id: 1, name: "Ada" }; }, the declared shape of the call is Promise<{ id: number; name: string }>, not the object inside it. Most of the time you want the inner type: the thing you receive after await. This post shows how to get the return type of an async function with ReturnType and Awaited, why ReturnType alone is not enough, and how the technique behaves with generic functions, overloads and functions that throw. The examples target TypeScript 6.x and work in the native TypeScript 7 compiler. The two utilities are documented in the utility types reference.

ReturnType gives you the promise

ReturnType<F> takes a function type and produces the type of what a call returns. For an async function that is a Promise.

promise-type.tsTypeScript
async function loadUser() {
  return { id: 1, name: "Ada" };
}

type Raw = ReturnType<typeof loadUser>;

const p: Raw = loadUser();
console.log(p instanceof Promise);
Text
true

Raw is Promise<{ id: number; name: string }>. That is accurate, and it is the right type if you want to store the promise or pass it to Promise.all. It is the wrong type if you want to declare a variable that holds the result of await loadUser(). For that you need to unwrap the promise.

Awaited unwraps it

Awaited<T> models what await does to a type. It removes the promise layer, and it keeps removing layers if the result is itself a promise or a thenable. It was added in TypeScript 4.5, and it is the supported way to express the unwrapping.

awaited.tsTypeScript
async function loadUser() {
  return { id: 1, name: "Ada" };
}

type User = Awaited<ReturnType<typeof loadUser>>;

function greet(user: User): string {
  return `Hello, ${user.name}`;
}

async function main() {
  const user = await loadUser();
  console.log(greet(user));
}

main();
Text
Hello, Ada

User is { id: number; name: string }. The function loadUser is the single source of the shape, and greet follows it. If you add a field to the object that loadUser returns, User gains the field with no edit anywhere else. This is the main reason to derive the type rather than write an interface that mirrors the function.

The order of the two utilities matters. Awaited<ReturnType<typeof f>> takes the return type and unwraps it. Writing ReturnType<Awaited<typeof f>> makes no sense, because a function type is not a promise.

A reusable helper

If you use the pattern in many places, give it a name.

async-return.tsTypeScript
export type AsyncReturn<F extends (...args: never[]) => Promise<unknown>> =
  Awaited<ReturnType<F>>;

async function fetchCount(table: string): Promise<number> {
  return table.length;
}

type Count = AsyncReturn<typeof fetchCount>;

const n: Count = 5;
console.log(n);
Text
5

The constraint accepts only functions that return a promise, so passing a synchronous function is a compile error that tells the reader what went wrong. The parameter list is typed never[] so that any argument list is assignable to it. Using any[] also works but weakens the checking, and never[] is the safer spelling for a constraint that must accept every function.

If you also want to allow plain functions, drop the Promise constraint and keep Awaited<ReturnType<F>>. Awaited leaves a non-promise type unchanged, so the helper works for both. That is a reasonable default for a general utility.

Explicit return types are respected

Writing the return type on the function makes the derived type stable. If you annotate Promise<User | null>, the helper gives you User | null and the null case is part of the contract.

nullable.tsTypeScript
interface User {
  id: number;
  name: string;
}

async function findUser(id: number): Promise<User | null> {
  return id === 1 ? { id, name: "Ada" } : null;
}

type Found = Awaited<ReturnType<typeof findUser>>;

function label(u: Found): string {
  return u === null ? "missing" : u.name;
}

console.log(label(await findUser(1)), label(await findUser(2)));
Text
Ada missing

The top-level await in the last line works in an ES module with a modern module setting. If your project uses CommonJS output, wrap the calls in an async function instead.

Without the annotation, the compiler infers the type from the return statements, which gives the same result here. An annotation is still worth writing on exported functions, because an accidental change to a return statement then shows up as an error at the function instead of in a distant caller.

Generic functions

For a generic async function, ReturnType cannot know the type argument, so it uses the constraint of each type parameter, or unknown if there is none.

generic.tsTypeScript
async function wrap<T>(value: T): Promise<{ value: T }> {
  return { value };
}

type Wrapped = Awaited<ReturnType<typeof wrap>>;

const w: Wrapped = { value: "anything" };
console.log(w.value);
Text
anything

Here Wrapped is { value: unknown }. If you need the result for a specific type argument, use an instantiation expression, which TypeScript supports since version 4.7.

instantiation.tsTypeScript
async function wrap<T>(value: T): Promise<{ value: T }> {
  return { value };
}

type WrappedNumber = Awaited<ReturnType<typeof wrap<number>>>;

const w: WrappedNumber = { value: 42 };
console.log(w.value.toFixed(1));
Text
42.0

typeof wrap<number> is the instantiation expression. It applies the type argument to the function type before ReturnType reads it, and WrappedNumber is { value: number }.

Overloads

For an overloaded function, ReturnType uses the last overload signature. That surprises people.

overloads.tsTypeScript
function parse(input: string): Promise<number>;
function parse(input: number): Promise<string>;
async function parse(input: string | number): Promise<number | string> {
  return typeof input === "string" ? Number(input) : String(input);
}

type Parsed = Awaited<ReturnType<typeof parse>>;

const p: Parsed = "text";
console.log(p);
Text
text

Parsed is string, because the last overload is the one that returns Promise<string>. If you need the type of a specific overload, give it its own named type or split the overloads into separate functions. The implementation signature is not visible to callers, so it is not used here either.

What the type does not include

The return type describes the fulfilled value only. A function can reject with any value, and TypeScript has no checked exceptions, so Awaited<ReturnType<F>> says nothing about errors. If your function reports failure through its resolved value, for example Promise<Result<User, Error>>, then the failure is part of the type. If it throws, the catch clause variable is unknown, and you narrow it there.

A second limit is that the derived type reflects the declaration, not the data. If the function parses JSON and returns as User, the type says User whether or not the data matches. Derivation only keeps types consistent with the code. It does not validate values at run time.

Using it for tests and mocks

The derived type is handy when you write a stub for tests. A fake that has to match the real function's result can borrow the type and stay in step.

stub.tsTypeScript
import type { loadUser } from "./user.ts";

type User = Awaited<ReturnType<typeof loadUser>>;

export const fakeUser: User = { id: 99, name: "Test User" };

export async function fakeLoadUser(): Promise<User> {
  return fakeUser;
}

If the real function later returns an extra required field, the stub fails to compile until you add it, which is exactly the prompt you want.

Combining with Promise.all and arrays

When several async functions run together, the derived types compose. A function that loads a user and their orders in parallel returns a tuple, and you can describe it without repeating either shape.

parallel.tsTypeScript
async function loadUser() {
  return { id: 1, name: "Ada" };
}

async function loadOrders() {
  return [{ id: "o1", total: 20 }];
}

async function loadAll() {
  const [user, orders] = await Promise.all([loadUser(), loadOrders()]);
  return { user, orders };
}

type Dashboard = Awaited<ReturnType<typeof loadAll>>;

const d: Dashboard = {
  user: { id: 2, name: "Grace" },
  orders: [{ id: "o2", total: 5 }],
};

console.log(d.orders[0].total);
Text
5

Promise.all accepts a tuple of promises and returns a promise of a tuple of the resolved values, so user and orders keep their exact types. The Dashboard type is derived through two functions, and a change to either shape flows into it. Writing that interface by hand would mean three places to keep in step.

Deriving the type of a class method

Methods work the same way, because a method is a function type once you reach it through the class type. Index the instance type with the method name and pass that to the utilities.

method.tsTypeScript
class UserService {
  async find(id: number) {
    return { id, active: true };
  }
}

type Found = Awaited<ReturnType<UserService["find"]>>;

const f: Found = { id: 3, active: false };
console.log(f.active);
Text
false

Note that no typeof is needed here. UserService["find"] is already a type, namely the type of the method, so it goes straight into ReturnType. Use typeof only when you start from a value such as a standalone function or an object instance, and use indexed access when you start from a type.

Also inAsync
Share

Keep reading

Flat illustration of an editor window full of code, with the TypeScript, npm and Node.js logos.
TypeScript

Type Inference in TypeScript

TypeScript works out most types for you. Learn how inference treats let and const, arrays, return values and callbacks, and when to write a type yourself.

7 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.