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

150
Visualizações
Check if object is of type (type is defined as array) - TypeScript

Let's say I have this

export type Hash = [ hashtype, hash ];

export type hashtype = -16 | -43 | 5 | 6;
export type hash = Buffer;

I want to write something that will check whether an object is a Hash

not implemented

isHash = (obj: any) => {
    return (obj is Hash) // pseudo code, to implement
}

So that I would have such a return:

isHash(5)                         => false    //  no hash
isHash([25, <Buffer ad 30>])      => false    //  25 is not in hashType

isHash([5, <Buffer ad 30>])       => true     //  valid
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There isn't a general-purpose way to check whether a type matches. For your specific case, I would do something like this:

const isHash = (obj: unknown): obj is Hash => {
  if (!Array.isArray(obj)) {
    return false;
  }
  if (obj.length !== 2) {
    return false;
  }
  return [-16, -43, 5, 6].includes(obj[0]) && Buffer.isBuffer(obj[1]);
}
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