Extracting Function Parameter Types
You'll learn how to use infer to pull out a function's parameter types into a reusable tuple type, understanding the building blocks behind TypeScript's built-in Parameters utility.
Why Extract Parameters?
When you're working with functions, sometimes you need to reference their parameter types separately from the function itself. Maybe you're building wrapper functions, creating mock implementations, or need to store arguments for later use. Manually duplicating those parameter types creates maintenance headaches—change the function, and you must update types elsewhere.
TypeScript gives us a way to extract those parameter types automatically.
The Core Pattern
You already know infer can extract array elements and return types. The same conditional type mechanism works for parameters:
type GetParameters<T> = T extends (...args: infer P) => any ? P : never;
Let's break this down:
T extends (...args: infer P) => anychecks ifTis a function...args: infer Psays "whatever the parameters are, capture them as typeP"- If it matches, return
P(the parameter tuple) - Otherwise, return
never
Seeing It in Action
type GetParameters<T> = T extends (...args: infer P) => any ? P : never;
function greet(name: string, age: number): void {}
type GreetParams = GetParameters<typeof greet>;
// Result: [name: string, age: number]
const args: GreetParams = ["Alice", 30]; // ✓ Valid
The extracted type P is a tuple matching the original parameters exactly—position, types, even optional or rest parameters.
Why This Matters
This pattern is the foundation of TypeScript's Parameters<T> utility type (which you've seen before). By understanding how infer captures parameter types, you can build custom utilities that manipulate function signatures, create type-safe wrappers, or enforce argument contracts.
Key Takeaway: Use infer in a function's parameter position to extract all parameter types as a tuple, enabling type-safe function manipulation without duplicating signatures.