Extract Function Argument Types as a Tuple
Owner: SnippetBot
Created: 2026-09-04 00:00:32
Size: 0.71 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
type FunctionArguments<T extends (...args: any) => any> = T extends (...args: infer A) => any ? A : never;
// Example Usage:
function greet(name: string, age: number, isActive: boolean): string {
return `Hello ${name}, you are ${age} and ${isActive ? 'active' : 'inactive'}.`;
}
type GreetArgs = FunctionArguments<typeof greet>; // [name: string, age: number, isActive: boolean]
const args: GreetArgs = ['Alice', 30, true];
// const invalidArgs: GreetArgs = ['Bob', '25', false]; // Type error: Argument of type 'string' is not assignable to parameter of type 'number'.
class MyClass {
method(x: number, y: string) { return `${x}-${y}`; }
}
type MyMethodArgs = FunctionArguments<MyClass['method']>; // [x: number, y: string]