What does the Parameters utility type do?
Parameters<typeof fn> returns the parameter types of a function as a tuple, for example [string, number]. You can index it, like Parameters<typeof fn>[0], to get one argument's type, which helps when wrapping functions.
Explain it simply
A function's argument list is a small fixed-length group, so TypeScript represents it as a tuple. Parameters hands you that tuple.
This is handy when writing a wrapper, such as a function that logs and then calls another one. Instead of copying the argument types, you derive them, so the wrapper stays correct if the original changes.
Example
function book(train: string, seats: number, tatkal: boolean) {
return `${train}:${seats}:${tatkal}`;
}
type BookArgs = Parameters<typeof book>;
// [train: string, seats: number, tatkal: boolean]
type FirstArg = Parameters<typeof book>[0]; // string
function withLog<F extends (...args: any[]) => any>(fn: F) {
return (...args: Parameters<F>): ReturnType<F> => {
console.log("calling with", args);
return fn(...args);
};
}
const loggedBook = withLog(book);
loggedBook("12951", 2, false); // arguments are type-checkedWhat to say in the interview
"Parameters<F> gives a function's argument types as a tuple. I use it in wrappers and decorators together with ReturnType so the wrapper has exactly the original signature without copying it by hand."