I am currently developing an open-source library that creates Proxies for Rx Observables.
(For the sake of simplicity, I will use Box<A>
instead of Observable<A>
)
The library takes a Box<A>
as input and returns a type
{ [P in keyof A]: Box<A[P]> }
.
There is one important detail: all methods on this new type must also return a Box
of the method result while maintaining their parameter types. For example:
Box<{ m(a:string):any }>
// becomes
{ m(a:string):Box<any> }
However, I am encountering an issue when attempting to proxy A
which has overloaded methods. The problem lies in redefining only one individual method definition within the overload group.
To simplify the issue:
type Proxy<O> = { [P in keyof O]: Wrap<O[P]> }
type Wrap<O> = O extends (...a: infer P) => infer R
? (...args: P) => Proxy<R>
: Proxy<O>
class A {
m(a: number): any;
m(a: string): any;
m(...a:any[]) {}
}
let p = {} as Proxy<A>;
// p.m resolves to a single override
// m(a: string): void;
p.m('1') // > returns Proxy<any>
p.m(1) // > Error: 1 is not string
Try this snippet in TS playground
Is it even possible?
Any assistance with resolving these typing issues would be greatly appreciated. Thank you!
Notes:
Everything works perfectly without typings!
You can find the library here: rxjs-proxify (types defined in src/proxify.ts
)