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

204
Visualizações
Why typescript doesn't know that i checked for object type?

I want to build such a function:


const recursionProxy = <T extends object>(subject: T) =>
  new Proxy(subject, {
    get(target, key: keyof T) {
      const nestedSubject = target[key];

      if (typeof nestedSubject === "object") {
        return recursionProxy(nestedSubject);
      }

      return nestedSubject ?? target._ ?? "Message not set";
    },
  });

but under the line recursionProxy(nestedSubject); there is an error that says

[i] Argument of type 'T[keyof T]' is not assignable to parameter of type 'object'.

why typescript doesn't take that if staement in consideration, in side the if statement nestedSubject is of type object

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It does seem to work if you use a type predicate :

const isObject = (f: any): f is object => {
   if(typeof f === "object") return true;
return false;
}
const recursionProxy = <T extends object>(subject: T) =>
  new Proxy(subject, {
    get(target, key: keyof T) {
      const nestedSubject = target[key];

      if (isObject(nestedSubject)) {
        return recursionProxy(nestedSubject);
      }

      return nestedSubject ?? target._ ?? "Message not set";
    },
  });

Link

A null check can also be added in the predicate, if that is required:

const isObject = (f: any): f is object => {
  if(f === null) return false;
   if(typeof f === "object") return true;
return false;
}
about 4 years ago · Juan Pablo Isaza Relatório
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