Reuse an API result type
Keep a separately declared state, cache, or adapter type synchronized with a function's inferred result.
TypeScript · Type Alias
ReturnType<Type> extracts the return type of a function type into a reusable type. It is globally available and was introduced in TypeScript 2.8.
ReturnType<Type> is a built-in conditional utility type that extracts the result type from a function type. Pass a function declaration with typeof, or another function type directly; overloaded functions use the last signature for inference.
ReturnType<Type>| Name | Type | Description |
|---|---|---|
Type | function type | A function type whose return type should be extracted. |
A type representing the return type of Type.
function loadUser(id: number) {
return { id, name: `user-${id}` };
}
type User = ReturnType<typeof loadUser>;
const user: User = loadUser(42);
console.log(user.name);The alias derives the object type returned by loadUser, so user is checked against that inferred shape.
Keep a separately declared state, cache, or adapter type synchronized with a function's inferred result.
Create a type from a named function without duplicating its implementation signature.
Model the result of an overloaded API when the documented final signature is authoritative.
ReturnType<Type> constructs a type from the return type of a function type. For a named function value, use ReturnType<typeof fn> so typeof supplies its function type.
Follow-up: What happens with an overloaded function? ReturnType infers from the last signature, which is generally the catch-all overload.
It is a type-level utility, so it does not call the function or change runtime behavior. It reuses the function's declared or inferred result type elsewhere in the program.
Follow-up: How can you obtain the return type of an inline function type? Write ReturnType<() => Result>, or use a compatible function type directly.
The utility was released in TypeScript 2.8. It is globally available as one of TypeScript's predefined utility types.
Follow-up: Which related utility extracts a constructor's instance type? InstanceType performs that analogous operation for constructor functions.
InstanceTypeParametersConstructorParametersAwaited