Edit
Since writing the original answer, typescript now has a built-in type (defined in lib.d.ts
) to get the type of the parameters called Parameters
type argsEmpty = Parameters<() => void> // []
type args = Parameters<(x: number, y: string, z: boolean) => void> // [number, string, boolean]
type argsOpt = Parameters<(x: number, y?: string, z?: boolean) => void> // [number, (string | undefined)?, (boolean | undefined)?]
Edit Typescript 3.0 has been relesed the code below works as expected.
While this is not possible in the current version of typescript (2.9) without spelling out all parameters. It will become possible in the next version of typescript (3.0) which will be released in the next few days:
type ArgumentsType<T> = T extends (...args: infer U) => any ? U: never;
type argsEmpty = ArgumentsType<() => void> // []
type args = ArgumentsType<(x: number, y: string, z: boolean) => void> // [number, string, boolean]
type argsOpt = ArgumentsType<(x: number, y?: string, z?: boolean) => void> // [number, (string | undefined)?, (boolean | undefined)?]
If you install npm install typescript@next
you can already play with this, it should be available sometime this month.
Note
We can also spread a tuple into arguments with this new feature:
type Spread<T extends any[]> = (...args: T)=> void;
type Func = Spread<args> //(x: number, y: string, z: boolean) => void
You can read more about this feature here