title: TypeScript: Conditional return type depending on one of the parameters value
body: <p>Could somebody help me with this:
  I have one of the parameters of enum type and depending on value of the enum I would need to specify the return type.</p>
  <p>I was trying something like this, but it didnt work.</p>
  <p>For example <code>parseQueryFilterValue(&quot;5&quot;, FilterType.Number)</code> needs to return type of <code>number | null</code></p>
  <pre><code>export enum FilterType {
    String = 'STRING',
    Number = 'NUMBER',
    StringArray = 'STRING_ARRAY',
    NumberArray = 'NUMBER_ARRAY',
  }
  
  type ValueReturnType = {
    [FilterType.String]: string | null
    [FilterType.Number]: number | null
    [FilterType.StringArray]: string[] | null
    [FilterType.NumberArray]: number[] | null
  }
  
  function parseQueryFilterValue&lt;T extends FilterType&gt;(
    queryValue: string | string[] | undefined,
    type: T
  ): ValueReturnType[T] {
    if (!queryValue || typeof queryValue !== 'string') {
      return null
    }
    switch (type) {
      case FilterType.Number:
        return +queryValue
      case FilterType.String:
        return queryValue
      case FilterType.StringArray:
        return queryValue.split('--')
      case FilterType.NumberArray:
        return queryValue.split('--').map((el) =&gt; +el)
    }
  }
  
  export default parseQueryFilterValue
  </code></pre>