Course contentsShow
← TypeScript
Lesson 1437 of 1,79633. Inference Inversion PatternsFree lesson

Extracting Function Parameter Types

Using infer to pull parameter types from function signatures, building toward a custom Parameters utility.

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) => any checks if T is a function
  • ...args: infer P says "whatever the parameters are, capture them as type P"
  • 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.