Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save binjospookie/21560e5da38d1fdf96beeb28dd3b0fed to your computer and use it in GitHub Desktop.
Save binjospookie/21560e5da38d1fdf96beeb28dd3b0fed to your computer and use it in GitHub Desktop.
kek.ts
/**
* Type the `filterTable` function to take a Table,
* a list of column names, and to return a table that
* only contains columns with these names.
*/
type FilterTable<Table, NameUnion> = TODO;
declare type UserTable = [
{ name: 'firstName'; values: string[] },
{ name: 'lastName'; values: string[] },
{ name: 'age'; values: number[] },
];
type res1 = FilterTable<UserTable, 'age'>;
type res2 = FilterTable<UserTable, 'firstName' | 'lastName'>;
type res3 = FilterTable<UserTable, 'firstName' | 'age'>
type res4 = FilterTable<UserTable, ''>
@binjospookie
Copy link
Author

type FilterTable<Table, NameUnion> = Table extends [infer R, ...infer Rest]
    ? R extends { name: NameUnion }
      ? [R, ...FilterTable<Rest, NameUnion>]
      : FilterTable<Rest, NameUnion>
    : [];
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment