title: Typescript: Return type of function based on input value (enum)
body: <p>I'm storing some settings into local storage and I would like to type the responses when I get (and ideally also insert) values from/to the storage.</p>
  
  <p>From what I've seen, the best way seems to be to use function overloading. So this is what I have now and it works:</p>
  
  <pre><code>export enum SettingsKey {
    hasOnboarded = 'hasOnboarded',
    phoneNumber = 'phoneNumber'
  }
  
  export async function getSetting(storage: Storage, key: SettingsKey.phoneNumber): Promise&lt;string&gt;
  export async function getSetting(storage: Storage, key: SettingsKey.hasOnboarded): Promise&lt;boolean&gt;
  export async function getSetting(storage: Storage, key: any) {
    return storage.get(key)
  }
  </code></pre>
  
  <p>The thing that I don't like about this solution is that it's possible to forget adding a new element in the enum to the overload type definitions. Is there a way to enforce that all enum values are handled? Or is there maybe a better way to do this altogether?</p>
  
  <p>I thought this would be a simple thing, a mapping from value <code>hasOnboarded</code> to return type <code>boolean</code> etc, but it's obviously not that easy.</p>
  
  <p>It looks to me like <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types" rel="noreferrer">conditional types</a> might solve this problem, but I can't quite wrap my head around how it works.</p>
  
  <p>I also saw <a href="https://medium.com/@KevinBGreene/typescript-enums-and-polymorphism-with-type-matching-fc3dc74b031c" rel="noreferrer">this</a> approach, but this seems like a little too much overhead.</p>
  
  <p>Any insight would be greatly appreciated!</p>