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

158
Views
How to look for an item in DB when the type is String | String[] using typescript union types

I am calling a method sendEmail that sends an email to a single user or a bunch of users(the type of to email is string | string[])

Is there a cleaner/better way to determine if it's an array or a single user so that I can look for it in the DB accordingly? The code looks quite repetitive now.

  public async sendEmail(
    toEmail: string | string[],
  ): Promise<void> {
    if (!Array.isArray(toEmail)) {
      const user = await this.userRepository.getUserByEmail(toEmail);
      await checkIfPaymentMade(user);
    } else {
      for (const email of toEmail) {
       const user = await this.userRepository.getUserByEmail(toEmail);
        await checkIfPaymentMade(user);
      }
    }
  }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A few other options come to mind

  1. Extract the common code to its own function. There's still some duplication, but just one line instead of however long the helper function needs to be.
public async sendEmail(
  toEmail: string | string[]
): Promise<void> {
  if (!Array.isArray(toEmail)) {
    await doStuff(toEmail);
  } else {
    for (const email of toEmail) {
      await doStuff(email);
    }
  }
}

private async doStuff(toEmail: string): Promise<void> {
  const user = await this.userRepository.getUserByEmail(toEmail);
  await checkIfPaymentMade(user);
}
  1. Have the body of the function only work with standalone strings. If it's an array, recurse.
public async sendEmail(
  toEmail: string | string[],
): Promise<void> {
  if (Array.isArray(toEmail) {
    for (const email of toEmail) {
      await sendEmail(email)
    }
  } else {
    const user = await this.userRepository.getUserByEmail(email);
    await checkIfPaymentMade(user);
  }
}
  1. Have the body of the function only work with arrays. If it's not an array, turn it into one first.
public async sendEmail(
  toEmail: string | string[],
): Promise<void> {
  const emailArray = Array.isArray(toEmail) ? toEmail : [toEmail];

  for (const email of emailArray) {
    const user = await this.userRepository.getUserByEmail(email);
    await checkIfPaymentMade(user);
  }
}
about 4 years ago · Juan Pablo Isaza Report

0

If you just want to keep it DRY, here's an option:

TS Playground link

public async sendEmail (recipients: string | string[]): Promise<void> {
  for (const address of Array.isArray(recipients) ? recipients : [recipients]) {
    const user = await this.userRepository.getUserByEmail(address);
    await checkIfPaymentMade(user);
  }
}
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!