title: TypeScript - narrowing type from generic union type
body: <pre><code>//Type declaration:
  
  interface TickListFilter {
     type: &quot;tickList&quot;;
     value: string;
  }
  
  interface ColorFilter {
     type: &quot;color&quot;
     value: ColorValueType
  }
  
  type Filter = TickListFilter | ColorFilter;
  
  
  
  ...
  onValueChange = (filter: Filter, newValue: Filter[&quot;value&quot;]) =&gt; {
          if (filter.type === &quot;tickList&quot;) {
              // variable 'filter' has TickListFilter type (OK)
              // variable 'filter.value' has string type (OK)
              // variable newValue has ColorValueType | string. I know why, let's fix it!
          }
  ...
  
  onValueChange = &lt;T extends Filter&gt;(filter: T, newValue: T[&quot;value&quot;]) =&gt; {
          if (filter.type === &quot;tickList&quot;) {
              // variable 'filter' has Filter type (wrong... i need tick list)
              // variable 'filter.value' has string | ColorValueType type (wrong, it should be string)
              // variable newValue has ColorValueType | string.
          }
  </code></pre>
  <p>How I can fix that? I know why it happens, because TS cant discriminate union by generic type. But is there any workaround to work as I described?</p>
  <p>I want to use similar structure as switch-case (not if-else only)</p>