title: Typescript extract exact Discriminated conditional unions type in iterator method
body: <pre><code>export type FILTER_META =
    | {
        type: 'string';
        key: string;
        filters: { id: string; label?: string }[];
      }
    | {
        type: 'time';
        key: string;
        filters: { min: string; max: string  }[];
      }
    | {
        type: 'range';
        key: string;
        filters: { min: number; max: number }[];
      };
  
   type Unpacked&lt;T&gt; = T extends (infer U)[] ? U : T;
   type Foo = Unpacked&lt;FILTER_META['filters']&gt;;
  
  // how to determine comparer type from meta object
  // comparer:Foo doesn't work
  // const comparator = &lt;T extends FILTER_META, U extends Unpacked&lt;T['filters']&gt;&gt;(
  
  const comparator = (meta: FILTER_META, item: any, comparer: any) =&gt; {
    const DATE_PREFIX = '1/1/2022 ';
  
    switch (meta.type) {
      case 'string':
        return item?.[meta.key]?.toLowerCase() === comparer.id?.toLowerCase();
      case 'time': {
        const { min, max } = comparer;
        const compTime = new Date(DATE_PREFIX + item?.[meta.key]);
        return (
          new Date(DATE_PREFIX + min) &lt;= compTime &amp;&amp;
          compTime &lt;= new Date(DATE_PREFIX + max)
        );
      }
      case 'range': {
        const { min, max } = comparer;
        const compItem = item?.[meta.key];
        return min &lt;= compItem &amp;&amp; compItem &lt;= max;
      }
    }
  };
  
  
  const genericFilter =
    (filterMeta: FILTER_META[]) =&gt;
    (list = []) =&gt;
      list.filter((item) =&gt;
        filterMeta
          .filter((fMeta) =&gt; fMeta.filters.length)
          .every((meta) =&gt;
            meta.filters.some((ft: any) =&gt; comparator(meta, item, ft))
          )
      );
  </code></pre>
  <p>The above is generic filter fn, which is trying to filter an array based on filter type.
  Its provided with array of different filter, which uses the comparer fn to filter out.</p>
  <p>How to type the 3rd argument <code>comparer:any</code> from the 1st argument of type <code>FILTER_META</code></p>
  <p>Stackblitz Link
  <a href="https://stackblitz.com/edit/typescript-ku6bq7" rel="nofollow noreferrer">https://stackblitz.com/edit/typescript-ku6bq7</a></p>