Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!