Personally, I like the readability of switch statements. Here is how to include functions as well in TypeScript.

Below is an example of how I integrated an API service result with a notification to the user. Why a switch statement here? Well, there are almost 10 result codes. This keeps readability great. Also, the default fallback for when new status codes would be added is pretty great.

switch (status) {
  case 'NOT_UNIQUE':
    this.notificationService.showError(`This account already exists.`);
    break;
  case 'PROHIBITED':
    this.notificationService.showError(`E-mail addresses from your domain are prohibited.`);
    break;
  default:
    this.notificationService.showError(`Something went wrong creating the account.`);
}

 

 

 

Saved you some valuable time?

Received 100 so far from 4 kind contributors.