Maybe you're looking for something in this style:
type Pop<T extends any[]> = T extends [...infer U, any] ? U : never
type RemoveLastParameter<T extends (...args: any[]) => any> =
(...args: Pop<Parameters<T>>) => ReturnType<T>
In this code snippet, the Pop
type extracts all elements from a tuple except the last one into U
and returns it.
Subsequently, RemoveLastParameter
can take any function type as input and generate a new function type with the last parameter removed using the Parameters
metafunction and Pop
.
A practical example can be seen below:
type Fn = (a: number, b: string, c: boolean) => void
type NewFn = RemoveLastParameter<Fn>
// (a: number, b: string) => void
declare const newFn: NewFn
newFn(1, 'abc') // works
Playground