Skip to content

Instantly share code, notes, and snippets.

@kirpalmakanga
Created February 6, 2024 17:53
Show Gist options
  • Save kirpalmakanga/72e7c4535babb86ac4603c295d432acd to your computer and use it in GitHub Desktop.
Save kirpalmakanga/72e7c4535babb86ac4603c295d432acd to your computer and use it in GitHub Desktop.
Type-safe pick function
export function pick<T extends object, K extends keyof T>(
base: T,
...keys: K[]
): Pick<T, K> {
if (!keys.length) return base;
const entries = keys.map((key) => [key, base[key]]);
return Object.fromEntries(entries);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment