Extract return type from async functions
Quick Answer
`AsyncReturnType<T>` unwraps the `Promise<X>` returned by an async function to give you the resolved type `X` directly.
1type AsyncReturnType<T extends (...args: any) => Promise<any>> =
2 T extends (...args: any) => Promise<infer R> ? R : any;
3
4// Usage
5async function fetchUser() {
6 return { id: 1, name: 'John' };
7}
8
9type User = AsyncReturnType<typeof fetchUser>; // { id: number; name: string; }The built-in ReturnType helper returns Promise<T> for async functions rather than the resolved T. AsyncReturnType unwraps the promise to give you the actual resolved type, which is what you usually want when typing variables, state, or props that will hold awaited values.
`Awaited<ReturnType<T>>` is the modern built-in equivalent and is preferred in TypeScript 4.5+. `AsyncReturnType` is useful in older codebases or for educational clarity.
This free typescript code snippet for async function type helper is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this intermediate-level example will help you implement async function type helper quickly and correctly.
All snippets in the Snippetly library follow typescript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses typescript.
Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.