TypeScript Reference ยท Utility Types
TypeScript ReturnType<T> utility type
Reference for TypeScript ReturnType<T>: get the return type of a function type, use it with typeof, handle overloads and generics, and avoid common errors.
ReturnType<T> extracts the type a function returns. T must be a function type, not a function value, so for a declared function you write ReturnType<typeof fn>. This lets you derive a type from an implementation instead of writing it twice.
It is common when a factory function builds a complex object and you want to name its shape, or when a library gives you a function whose result type is not exported.
function makePoint() { return { x: 0, y: 0 }; } type Point = ReturnType<typeof makePoint>; // { x: number; y: number }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
| Name | Description |
|---|---|
T |
A function type. It must be assignable to (...args: any) => any. |
Result: the return type R of T. For a void function it is void; for an async function it is Promise<...>.
Edge cases: ReturnType<any> is any, and ReturnType<never> is never. A non-function type such as string is a compile error.
Usage notes
- Use
typeoffor values.ReturnType<makePoint>is an error becausemakePointis a value; writeReturnType<typeof makePoint>. - Overloads. TypeScript infers from the last overload signature only, which may not be the one you expect.
- Generic functions. Type parameters in the signature are replaced by their constraint (
unknownwhen there is none), so you do not get a generic result. - Async functions. You get the
Promise. Wrap withAwaitedto get the resolved value:Awaited<ReturnType<typeof load>>. - Methods and classes. For a method, use
ReturnType<Foo["method"]>. For a constructor, useInstanceType;ReturnTypeof a class constructor is an error. - Stays in sync. Changing the function changes the derived type, which avoids duplicate interfaces. Prefer an explicit named type when the shape is part of a public API.
inferis the engine. The same technique extracts element types, promise values and more (see Conditional Types).- Available since TypeScript 2.8.