title: TypeScript: dynamic union type derived from an object value
body: <p>I want to derive a union type from a table-like input object. I've succeed with a working solution but I suppose there should be shorter / more straightforward way. Here's the input object:</p>
  <pre><code>const _COLUMNS = {
    &quot;id&quot;: {sortable: true}, // more metadata in real code
    &quot;@fullname&quot;: {sortable: true},
    &quot;@profiles&quot;: {sortable: false},
    &quot;created_at&quot;: {sortable: false},
    ...
  } as const
  </code></pre>
  <p>And here's my solution:</p>
  <pre><code>type Column = keyof (typeof _COLUMNS)
  
  // ---
  type FindSortable&lt;K extends string, T extends {sortable : boolean}&gt; = T extends {sortable : true} ? K : never
  
  type _Sortable = {
    [K in Column] : MakeSortable&lt;K, typeof _COLUMNS[K]&gt;
  }
  
  type Sortable = _Sortable[keyof _Sortable] // &quot;id&quot; | &quot;@fullname&quot;
  type Order = `${Sortable}:${&quot;asc&quot; | &quot;desc&quot;}` // &quot;id:asc&quot; | &quot;id:desc&quot; | ...
  //---
  </code></pre>
  <p>Is there a way to define the above dynamic <code>Sortable</code> type without a middleman <code>_Sortable</code>? Here's the repeating structure:</p>
  <pre><code>type Sortable = 
    | FindSortable&lt;&quot;id&quot;, typeof _COLUMNS[&quot;id&quot;]&gt;
    | FindSortable&lt;&quot;@fullname&quot;, typeof _COLUMNS[&quot;@fullname&quot;]&gt;
    ...
    | FindSortable&lt;K, typeof _COLUMNS[K]&gt; ??
  </code></pre>
  <p>Or in math-like notation:</p>
  <pre><code>type Sortable = FindSortable&lt;K, typeof _COLUMNS[K]&gt; ∀ K in Column ??
  </code></pre>