Last active
May 10, 2024 21:09
-
-
Save DanielBaulig/52da25af8a9697d2f5fb3908378fd093 to your computer and use it in GitHub Desktop.
grab utility function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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