function-types
ReturnType<T>
Infers the return type of a function type T using `infer R`. Widely used to derive types from existing functions without redeclaring them.
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any Usage patterns
| Pattern | Purpose |
|---|---|
| ReturnType<typeof fn> | Get a value's function return type via typeof |
| Awaited<ReturnType<T>> | Get the resolved type for async functions |
| ReturnType<T> extends Promise<infer R> ? R : ReturnType<T> | Manual async unwrap (pre-Awaited) |
| infer R | The inference site that names the return type |
Examples
function greet() { return 'hi' as const; }
type G = ReturnType<typeof greet>; Result: 'hi'
async function load() { return { ok: true }; }
type Loaded = Awaited<ReturnType<typeof load>>; Unwraps the Promise to { ok: boolean }
type Handler = (e: Event) => void;
type R = ReturnType<Handler>; Result: void
type BadReturn = ReturnType<string>; // Error T must be a function type — non-function inputs error at compile time
Gotcha
For overloaded functions, ReturnType only picks the last overload signature. For async functions it gives Promise
Related utility types
Parameters<T>
Extracts the parameters of a function type T as a tuple. Enables reusing a function's argument list for wrappers, currying, or higher-order utilities.
Awaited<T>
Models the type produced by awaiting T, recursively unwrapping nested Promises (and other thenables). Introduced in TS 4.5 and used internally by the `await` expression's type.
infer R
A keyword usable only inside the extends clause of a conditional type that introduces a fresh type variable inferred from the matched shape. It is the primitive that powers ReturnType, Parameters, InstanceType, Awaited, and countless custom utilities.
ConstructorParameters<T>
Extracts the parameter tuple of a class constructor T. Mirrors Parameters but operates on `new (...)` signatures instead of call signatures.
ThisParameterType<T>
Extracts the explicit `this` parameter type from a function T, or `unknown` if none is declared. Useful for reflecting on method-style callbacks that require a specific caller.
← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never