ReturnType<T>
Extracts the return type of a function type T, enabling you to reuse a function's output type without manual duplication.
Definition
type ReturnType<T extends (...args: any) => any> =
T extends (...args: any) => infer R ? R : any;Examples
function fetchUser() {
return {
id: 1,
name: "Alice",
email: "[email protected]",
roles: ["admin", "user"] as const,
};
}
type User = ReturnType<typeof fetchUser>;
// { id: number; name: string; email: string; roles: readonly ["admin", "user"] }async function getProducts() {
const response = await fetch("/api/products");
return response.json() as Promise<{ id: string; name: string }[]>;
}
type ProductsResult = ReturnType<typeof getProducts>;
// Promise<{ id: string; name: string }[]>
type Products = Awaited<ReturnType<typeof getProducts>>;
// { id: string; name: string }[]const selectors = {
getUser: (state: AppState) => state.user,
getTheme: (state: AppState) => state.settings.theme,
getItems: (state: AppState) => state.items.filter(i => i.active),
};
type UserFromStore = ReturnType<typeof selectors.getUser>;
type ThemeFromStore = ReturnType<typeof selectors.getTheme>;Common Use Cases
- 1Deriving types from existing function implementations
- 2Typing variables that hold function results
- 3Building type-safe store selectors
- 4Creating mock return types for testing
- 5Avoiding manual duplication of complex return shapes
Understanding ReturnType<T>
ReturnType<T> extracts the return type of a function type T. It uses a conditional type with infer to capture the return type from the function signature. This utility type is one of the most frequently used in TypeScript codebases.
The primary benefit is avoiding type duplication. When a function computes and returns a complex object, you don't need to separately define an interface for that object. Instead, ReturnType<typeof myFunction> infers the shape directly from the implementation. If the function's return shape changes, all dependent types update automatically.
ReturnType is commonly used with typeof to extract types from value-level functions. This is especially valuable with third-party libraries where return types might not be explicitly exported. Rather than digging through library source code, Parameters<typeof fn> and ReturnType<typeof fn> give you everything you need.
For async functions, ReturnType returns the Promise-wrapped type. To unwrap the promise and get the actual resolved type, combine it with Awaited: Awaited<ReturnType<typeof asyncFn>>. This pattern is extremely common in Next.js applications where server functions return promises.
In state management, ReturnType is used to derive selector result types. A selector function takes state and returns a derived value. Using ReturnType on the selector gives you the shape of that derived value without manually maintaining a separate type definition.
ReturnType also enables powerful higher-order function patterns. A memoize function, for example, can use ReturnType to ensure the cached value has the correct type, providing end-to-end type safety through the caching layer.
Related Types
Parameters<T>Extracts the parameter types of a function type T as a tuple, enabling you to reuse a function's argument types elsewhere.
Awaited<T>Recursively unwraps the resolved type of a Promise or nested Promises, giving you the eventual value type.
InstanceType<T>Extracts the instance type of a constructor function type T, giving you the type of objects created by the constructor.
infer keywordThe infer keyword declares a type variable within a conditional type's extends clause, extracting and capturing part of a type for use in the true branch.
More Object & Function Types
Explore TypeScript Types
Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.