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

290
Visualizações
How to differentiate between Blob, Object and String?

Lets say some function can return Blob, String or plain Object, depending on the use case. After the value is returned I want to implement different code for each type. How can I tell them apart?

For simplification I commented the 3 options that can return from the manage function, but this is the way I manage it right now:

const manage = () => {
    
  return new Blob([JSON.stringify({ myKey: 'myValue'})], { type: 'application/json' });
  
  // return { error: 'my error msg' };
  
  // return 'lets say this is base64 string';
};

const result: Blob | { error: string } | string = manage();

switch(Object.prototype.toString.call(result)) {
    case "[object Blob]":
    console.log('Blob case runs...');
    break;
  case "[object String]":
    console.log('String case runs...');
    break;
  case "[object Object]":
    console.log('Object case runs...');
    break;
  default: console.log('Default case runs...');
 };
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Obviously I am not happy with the "Object.prototype.toString.call" solution. Also some answers suggested to use the "instance of" operator, but this is also not a reliable option in my situation.

Can someone point me in a better direction?

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

0

you can just return the type of data and the data in the manage function

const manage = () => {
    
  return {
    type: "Bolb",
    data: new Blob([JSON.stringify({ myKey: 'myValue'})], { type: 'application/json' });
  }
   return {type: "error", error: 'my error msg' };
  
   return {type: "string", message: 'lets say this is base64 string'};
};

const result: Object = manage();

switch(result.type) {
    case "Bolb":
    console.log('Blob case runs...');
    break;
  case "string":
    console.log('String case runs...');
    break;
  case "error":
    console.log('Object case runs...');
    break;
  default: console.log('Default case runs...');
 };
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