Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
function makePoint() {
  return { x: 0, y: 0 };
}

type Point = ReturnType<typeof makePoint>;
// { x: number; y: number }

Try it

TypeScript Naming an inferred type

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.

TypeScript Overloads, generics and errors

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

TypeScript
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 typeof for values. ReturnType<makePoint> is an error because makePoint is a value; write ReturnType<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 (unknown when there is none), so you do not get a generic result.
  • Async functions. You get the Promise. Wrap with Awaited to get the resolved value: Awaited<ReturnType<typeof load>>.
  • Methods and classes. For a method, use ReturnType<Foo["method"]>. For a constructor, use InstanceType; ReturnType of 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.
  • infer is the engine. The same technique extracts element types, promise values and more (see Conditional Types).
  • Available since TypeScript 2.8.