1

This seems like a simple question but I haven't found an answer or more likely I'm not sure the correct thing to look up.

In my typescript code I have a type called Determiner that resembles the following:

type Determiner = 'a' | 'an' | 'the' | '' | 'auto';

In my code I have a variable of type Determiner and I am reassigning it based on the value of another variable that is of type string.

let determiner: Determiner = 'auto';
const content: string = getNewValue();

Right now the way I'm checking is a bit inefficient by using an if statement with all of the valid values.

if (
  content === "a" ||
  content === "an" ||
  content === "the" ||
  content === "" ||
  content === "auto"
) {
  determiner = content;
}

While this works, I'm wondering if there's an easier to perform this check without enumerating all of the possible values manually.

Abir Taheer
  • 2,502
  • 3
  • 12
  • 34
  • 1
    Translating the answer from the question this duplicates (read [this](https://stackoverflow.com/a/51529486/2887218) starting with "what you *can* do...") here gives [this code](https://tsplay.dev/8w80rw) – jcalz Dec 22 '20 at 21:13
  • @jcalz Yep, I figured it out. Thank you! – Abir Taheer Dec 22 '20 at 21:13

0 Answers0