Skip to content

Instantly share code, notes, and snippets.

@DanielBaulig
Last active May 10, 2024 21:09
Show Gist options
  • Save DanielBaulig/52da25af8a9697d2f5fb3908378fd093 to your computer and use it in GitHub Desktop.
Save DanielBaulig/52da25af8a9697d2f5fb3908378fd093 to your computer and use it in GitHub Desktop.
grab utility function
// Grabs a given list of properties from an
// object and returns a new "shrunk" object
// with just those properties while preserving
// correct typing.
function grab<
O extends { [x in A[number]]: unknown },
const A extends string[]
>(o: O, properties: A): {
[x in A[number]]: typeof o[x]
} {
return Object.fromEntries(
Object.entries(o).filter(([k, ]) => properties.includes(k))
) as O;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment