I am trying to allow deep, remote storage access using native JS chaining syntax. I establish a proxy to build an access chain/route, then when the end of the chain is called, we would retrieve the object from storage, access the deep property and return it. A simplified case of this type of wildcard chaining is as follows:
If we define a class as follows
class TestClass{
private handler = {
chain: [],
get(target: TestClass, key: any){
chain.push(key);
return new Proxy(target, this);
},
apply(target, thisArg, argumentsList){
const lastArg = target[this.chain[this.chain.length - 1]
if(typeof target[lastArg] === "function")
return target[lastArg](...argumentsList);
console.log(this.chain);
this.chain = [];
}
}
constructor(){
return new Proxy(this, this.handler)
}
actualFunction(){
console.log("Called actual function")
}
}
We could do something like:
let t = new TestClass();
t.a.b.c.func();
To have it log [a,b,c,func] or we could do
t.a.b.c.actualFunction()
To have it log Called actual function. The issue here is that we want to receive autocomplete and type hints using wildcard access, the only way I've found to enable this so far is to use the index definition [x: string|number|symbol]: TestClass at the top of the class. This allows wildcard access with type hints and code completion but throws a bunch of TypeScript errors within the class itself.
Optimally I could use an index definition along the lines of [x: string|number|symbol]: literalType x in keyof TestClass ? any : TestClass
Any suggestions on how to make this work are much appreciated. Additionally, design decisions/directions that may enable this easier are also welcome.