I'm trying to define a type which gets a function type as a generic parameter and returns a function type which is the same as the input function type except it has one more argument at the end:
type AugmentParam<F extends (...args: any[]) => any, ExtraParam> = F extends (
...args: infer Args
) => infer R
? (
...args: [
...Args,
ExtraParam
]
) => R
: never
An example of the usage:
type F = (x: number) => boolean
type F2 = AugmentParam<F, string> // (x: number, arg2: string) => boolean
...Args seems not working, however if I change it to something like this it works:
type AugmentParam<F extends (...args: any[]) => any, ExtraParam> = F extends (
...args: infer Args
) => infer R
? (
...args: [
Args[0],
Args[1] /* Spread doesn't work here, so it doesn't work for arbitrary number of arguments :( */,
ExtraParam
]
) => R
: never
But it only works for a specific number of arguments and I need to define one such type for each n-ary function.