Accessing Individual Parameters
What you'll learn: How to extract a single parameter's type by indexing into the Parameters<T> tuple.
The Core Idea
You already know that Parameters<T> gives you a tuple of all parameter types. Because it's a tuple, you can use numeric index access to extract just one parameter type at a specific position.
Think of it like reaching into a box of labeled items and pulling out exactly the third item — you use the index [2] (zero-based) to grab that specific type.
The Syntax
type MyFunc = (name: string, age: number, active: boolean) => void;
type AllParams = Parameters<MyFunc>;
// [name: string, age: number, active: boolean]
type FirstParam = Parameters<MyFunc>[0]; // string
type SecondParam = Parameters<MyFunc>[1]; // number
type ThirdParam = Parameters<MyFunc>[2]; // boolean
You use bracket notation with a numeric literal to index into the tuple, just like accessing array positions.
When This Is Useful
Imagine you're building a wrapper function but only need to validate or transform one specific argument:
type Handler = (id: string, payload: object) => void;
type PayloadType = Parameters<Handler>[1]; // object
function validatePayload(data: PayloadType): boolean {
// Now you're working with the exact type of the second parameter
return typeof data === 'object';
}
This keeps your types synchronized — if the Handler signature changes, PayloadType updates automatically.
Key Takeaway: Index into Parameters<T> tuples with [0], [1], etc., to extract individual parameter types by position, keeping derivative types in sync with the source function.