Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
Specifying type for wildcard access through proxy

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.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda