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

260
Views
How can I do type-guarding for the same object?

For example, I have such a code:

interface Subscriber {
  reviewData: string | string[]
  hasArray: boolean;
}

const reviews = ["Good", "Not bad"];

const subscriber: Subscriber = {
  reviewData: reviews,
  hasArray: Array.isArray(reviews)
}

if (subscriber.hasArray) {
  subscriber.reviewData.forEach(console.log);
} else {
  console.log(subscriber.reviewData);
}

Of course, TypeScript will complain at subscriber.reviewData.forEach(console.log); saying that it can't define whether it's an array or just a string because there's not enough type guarding in the condition subscriber.hasArray.

I don't want to always write Array.isArray(subscriber.reviewData) because I'm gonna use this logic in many other places.

I've heard of is but that's not really the case in my scenario because using is implies you have to have a function and pass some argument into it.

How can I achieve the desired result?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want to be able to use hasArray as a way to determine if reviewData is a string[] or a string, then you really want Subscriber to be a discriminated union type instead of an interface. Your interface would allow {reviewData: "", hasArray: true}, and so the compiler cannot discount the possibility. With a discriminated union you can have a discriminant property that you check in order to narrow the type of the whole object:

type Subscriber = {
    reviewData: string[]
    hasArray: true;
} | {
    reviewData: string
    hasArray: false;
}

In this case hasArray is the discriminant property of a boolean literal type true or false.

Then, given a Subscriber:

const subscriber: Subscriber = Math.random() < 0.5 ?
    { reviewData: "Good", hasArray: false } :
    { reviewData: ["Who", "Cares"], hasArray: true };

We can check it the way you want without compiler error:

if (subscriber.hasArray) {
    subscriber.reviewData.forEach(console.log);
} else {
    console.log(subscriber.reviewData.toUpperCase());
}

Playground link to code

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!