ReturnType, Parameters, Awaited
ReturnType gives the type of the value a function returns, Parameters a tuple of its argument types, and Awaited recursively unwraps a Promise. For an async function the result is Awaited<ReturnType<typeof fn>>, because ReturnType gives the Promise itself. This derives the result types of factories and fixtures from the implementation without a second source of truth. API contracts come from OpenAPI, not from the implementation, since an inferred type repeats its bugs.
TypeScript · Utility types
The result interface of a fixture was written by hand, the function changed, and the test still compiles. Where does the type come from?
A helper seedAccount creates a user through the API and returns an object. Next to it someone wrote a manual interface SeededAccount to pass that object into other functions. A month later the helper got a token field that the interface lacks, and the compiler does not notice the stale type. This lesson is about three utilities that derive types straight from functions: ReturnType, Parameters, Awaited.
How it works and why. ReturnType<typeof fn> takes the type of the value the function returns. Parameters<typeof fn> gives a tuple of argument types, keeping names and optionality. Both are built on infer in a conditional type, that is "substitute whatever is there". Awaited<T> (since TypeScript 4.5) recursively unwraps a Promise and any thenable. It is needed because an async function always returns a Promise, and ReturnType gives exactly that, not the result.
The result type of an async function
The difference between a value and a type matters here. The function seedAccount is a value, and in a type position it is taken with the typeof operator. The form ReturnType<seedAccount> without typeof does not compile.
Example: two sources of truth. A manual interface next to the function that produces it:
interface SeededAccount {
id: string;
role: string;
balance: number;
}
async function expectBalance(acc: SeededAccount, amount: number) {
const res = await api.get('/accounts/' + acc.id);
expect((await res.json()).balance).toBe(amount);
}
The role field in the interface is string while the function returns the narrower 'admin' | 'viewer'. The token field is absent from the interface altogether, so functions that need it must repeat as or carry the variable separately. Structural typing lets an object with an extra field be passed, so there is no error. The types drifted apart quietly.
The fix is to have no second source: derive the type from the implementation.
async function seedAccount(
role: 'admin' | 'viewer',
balance = 0,
) {
const res = await api.post('/test/accounts', {
data: { role, balance },
});
const { id, token } = await res.json();
return { id: id as string, role, balance, token: token as string };
}
type SeededAccount = Awaited<ReturnType<typeof seedAccount>>;
type SeedArgs = Parameters<typeof seedAccount>;
type SeededAccount = Awaited<ReturnType<typeof seedAccount>>;
async function expectBalance(acc: SeededAccount, amount: number) {
const res = await api.get('/accounts/' + acc.id);
expect((await res.json()).balance).toBe(amount);
}
Now a field added to the helper, token, is immediately visible in SeededAccount, and the narrowing of role is kept. This is especially handy for fixture results, where a separate interface would only duplicate code.
A wrapper that keeps the signature. Parameters and Awaited<ReturnType<F>> make it possible to write a wrapper that loses neither the arguments nor the result type, for example wrapping a call in test.step:
function withStep<F extends (...args: any[]) => Promise<unknown>>(
title: string,
fn: F,
) {
return async (
...args: Parameters<F>
): Promise<Awaited<ReturnType<F>>> => {
return test.step(title, async (): Promise<Awaited<ReturnType<F>>> => {
return (await fn(...args)) as Awaited<ReturnType<F>>;
});
};
}
const seed = withStep('Seed account', seedAccount);
const acc = await seed('admin', 500);
The constraint (...args: any[]) is a deliberate and accepted trick here: any function fits it, while the real argument and result types still come from F. The result of seed has the same parameters ('admin' | 'viewer', number?) and the same result type as seedAccount.
Where the type comes from
A separate interface
A second description next to the function. A field was added to the code and is absent from the interface, and nobody noticed.
Awaited<ReturnType<typeof fn>>
The type always matches what the function actually returns.
Limits and alternatives.
- A derived type follows the implementation, including its bugs. If the source of truth is an external contract (OpenAPI), types are generated from it, and then the implementation must match the contract, not the other way round.
- For a function with overloads,
ReturnTypeandParametersuse the last signature, not all of them. For a generic function, type parameters are replaced by their constraints. - For classes there are
ConstructorParametersandInstanceType, and forthisin a methodThisParameterType. - When many files use a function's type, it is handier to name it once in a module (
export type SeededAccount = …) and import it.
Common mistakes.
ReturnType<typeof asyncFn>withoutAwaited: the result is aPromise, and its fields are not reachable.- Forgetting
typeofbefore the function name. - Duplicating a function result in a separate
interfacethat later lags behind. - Deriving an API contract type from a test helper implementation: the type adapts to the bug and never shows a mismatch.
Check yourself
Why does ReturnType<typeof seedAccount> for an async function not give the needed fields, and what should be added?
Answer
Because an async function returns a Promise, and ReturnType returns it with the wrapper. Awaited unwraps the promise: Awaited<ReturnType<typeof seedAccount>>.
What happens to the type Awaited<ReturnType<typeof fn>> if the return type of fn changes?
Answer
The type changes with the function because it is derived from it. Every place that uses it either picks up the change automatically or gives a compile error if it no longer fits.
ReturnType gives the type a function returns, Parameters the type of its arguments, and Awaited unwraps a Promise. For an async function the result is Awaited<ReturnType<typeof fn>>. Middle/Senior: This is how I derive result types of factories and fixtures and write wrappers that keep the signature, with no second source of truth. But I take API contracts from OpenAPI, not from an implementation: a derived type repeats its bugs.