title: TypeScript extend from any string but override with type
body: <p>I have a function that checks to see if a generic type’s value exists in an array:</p>
  <pre><code>export function isValueInArray&lt;T extends string&gt;(
    values: T[],
    value: T | undefined
  ) {
    return value ? values.includes(value) : false;
  }
  </code></pre>
  <p>It would then be used like this:</p>
  <pre><code>export type Status = 'OPEN' | 'CANCELED' | 'ACCEPTED' | 'INVALIDATED';
  
  // typeof buyNowStatus === Status 
  const isValid = isValueInArray(['OPEN'], buyNowStatus)
  </code></pre>
  <p>In this case it works in terms of auto-completing the first array argument. The issue is that we can pass in any string:</p>
  <pre><code>// no typescript error is thrown here 
  const shouldBeInvalid = isValueInArray(['OPEN', 'ANY'], buyNowStatus)
  </code></pre>
  <p>One way to fix this would be to pass the Status in as a generic:</p>
  <pre><code>// ts errors out properly (but we must pass in a generic)
  const isInvalid = isValueInArray&lt;Status&gt;(['OPEN', 'ANY'], buyNowStatus)
  </code></pre>
  <p>Is there a way to achieve this without needing to pass <code>Status</code> in as a generic? To somehow infer the <code>Status</code> type from <code>buyNowStatus</code> and for that to override the default <code>T extends string</code> behaviour in <code>isValueInArray</code>?</p>
  <pre><code>function checkStatus(status: Status) {
    // no typescript error thrown here either
    return isValueInArray(['ANY'], status);
  }
  </code></pre>