TypeScript Reference ยท Utility Types
TypeScript Parameters<T> utility type
Reference for TypeScript Parameters<T>: get a function's parameter types as a tuple, index into it, and use it for wrappers, decorators and argument forwarding.
Parameters<T> turns the parameter list of a function type into a tuple type. The tuple keeps the order, optional markers, rest parameters and (in recent versions) the parameter names, so it is a faithful copy of the argument list.
Its main job is writing wrappers: a function that logs, caches or retries another function needs to accept exactly the same arguments, without you retyping them.
function greet(name: string, times?: number): void {} type GreetArgs = Parameters<typeof greet>; // [name: string, times?: number | undefined]
Try it
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.
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
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
| Name | Description |
|---|---|
T |
A function type. For a function value, use typeof fn. |
Result: a tuple type with one element per parameter. A function with no parameters gives [].
Edge cases: Parameters<any> is unknown[]; Parameters<never> is never. A non-function T is a compile error.
Usage notes
- Optional and default parameters become optional tuple elements (
times?: number). Defaults are not visible in types, so the element is optional with the declared type. - Rest parameters become rest elements of the tuple, so the tuple has no fixed length.
- Spread it back:
fn(...args)works whenargsis typed asParameters<typeof fn>. - Overloads: only the last overload signature is used, just like
ReturnType. - Generics: type parameters are replaced by their constraints in the tuple.
- Single argument:
Parameters<typeof f>[0]is the first parameter's type. Use it to name a type a library did not export. - Methods:
Parameters<Foo["method"]>ignores thethistype. SeeThisParameterType. - Constructors: use
ConstructorParameters<T>. - Available since TypeScript 3.1 (labelled tuple elements in 4.0).