In my current project, I have a unique design where a class contains instance methods that act as handlers, each representing a specific operation. These handlers take a reference as input and assign the output to a second parameter. To simplify this process, a proxy object is created using a third-party library, allowing direct invocation of the handlers.
type InputRef<T> = {
current: T,
};
type OutputRef<T> = {
current?: T,
};
class Original {
increment(input: InputRef<number>, output: OutputRef<number>) {
const { current: inValue } = input;
output.current = inValue + 1;
}
}
type Mapper<Fn> = Fn extends (input: InputRef<infer U>, output: OutputRef<infer V>) => unknown ? (input: U) => V : never;
type MyProxyGeneratedByThirdPartyJs = { [FnName in keyof Original]: Mapper<Original[FnName]> };
declare const proxy: MyProxyGeneratedByThirdPartyJs;
const result = proxy.increment(3); // 4
However, there is a limitation with the mapper function. It fails to support generic types, such as:
class Original {
toBox<T>(input: InputRef<T>, output: OutputRef<{ boxed: T }>) {
const { current: inValue } = input;
output.current = { boxed: inValue };
}
}
Due to this limitation, the type of proxy
only accepts unknown
, resulting in the loss of generic information like T
.
My goal is to redefine the type of proxy
to:
{
toBox<T>(input: T): { boxed: T },
}
Instead of the current definition which is:
{
toBox(input: unknown): { boxed: unknown },
}
Is there a possible solution to achieve this desired outcome?