Basically, I want something like this:
export type ReturnValueMapper<Func extends (...args: Args[] /* impossible */ ) => any, ReturnValue> = (...args: Args[]) => ReturnValue;
I'm almost sure that it's impossible, but I haven't found exact confirmation.
The use case is improving types for recompose's withStateHandlers, enabling defining state updaters like this:
interface StateUpdaters {
update(field: string): void; // I don't want to specify Partial<State> here
}
Edit
Since the original question was answered typescript has improved the possible solution to this problem. With the addition of Tuples in rest parameters and spread expressions we now don't need to have all the overloads:
Not only is this shorter but it solves a number of problems
Sample:
Original
For a good solution you will need variadic types, but for now this answer provides a workable solution. (Posting it here as the type there is used as part of a solution to a different question).
The basic idea is that we will extract the parameter types and recompose the function signature with the new return type. There are several disadvantages to this approach:
There may be other issues, but depending on your use-case this may be a good enough solution until the type system addresses this use case.
The issue when used with optional parameters is that the optional parameter becomes required (and is of
type A | undefined
):