title: How to type a Typescript array to accept only a specific set of values?
body: <p>I am writing a type declaration file for a library I do not control. One of the methods accepts an array of strings as a parameter, but these strings can only be very specific values. Currently I am typing this parameter as a <code>string[]</code>, but I was wondering if there was a way to enhance this to include the specific values as well.</p>
  
  <p>Example source (I cannot change this):</p>
  
  <pre><code>Fruits(filter) {
      for (let fruit of filter.fruits)
      {
          switch(fruit)
          {
              case 'Apple':
                  ...do stuff
              case 'Pear':
                  ...do stuff
              default:
                  console.error('Invalid Fruit');
                  return false;
          }
      }
      return true;
  }
  </code></pre>
  
  <p>My current type declaration:</p>
  
  <pre><code>function Fruits(filter: FruitFilter): boolean;
  
  interface FruitFilter {
      fruits: string[];
  }
  </code></pre>
  
  <p>As I was writing this question I came up with a partial solution by defining a union type of the strings that are valid, then setting the type of the field to an array of that union rather than an array of strings. This gives me the checking I want, but I noticed that if you enter an invalid string, it marks all of the strings in the array as invalid with the error <code>Type 'string' is not assignable to type 'Fruit'</code>. Is there a better way of doing this so that only the offending string is marked as invalid, or is this as close as I'm going to get?</p>
  
  <p>Partial solution:</p>
  
  <pre><code>function Fruits(filter: FruitFilter): boolean;
  
  type Fruit = 'Apple' | 'Pear'
  
  interface FruitFilter {
      fruits: Fruit[];
  }
  </code></pre>