Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

296
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda