Skip to content

Instantly share code, notes, and snippets.

@gcavanunez
Last active December 30, 2022 14:15
Show Gist options
  • Save gcavanunez/7e6ef78c99c4e9873353c07684a59b78 to your computer and use it in GitHub Desktop.
Save gcavanunez/7e6ef78c99c4e9873353c07684a59b78 to your computer and use it in GitHub Desktop.
Typescript patterns
const extractFromObj = <T, K extends keyof T>(
obj: T,
keys: K[]
): Pick<T, K> => {
return keys.reduce((newObj, curr) => {
newObj[curr] = obj[curr];
return newObj;
}, {} as Pick<T, K>);
};
type sProduceConditionsMapper<T> = {
[K in keyof T]?: string[];
};
const pickObjectKeys = <
T extends sProduceConditionsMapper<T>,
K extends keyof T
>(
obj: T,
keys: K[]
): { [O in K]: string[] } => {
return keys.reduce((newObj, curr) => {
// if (Array.isArray(obj[curr])) {
newObj[curr] = obj[curr] || [];
// newObj[curr] = obj[curr];
// }
return newObj;
}, {} as { [O in K]: string[] });
type ProduceConditionsMapper<T> = {
[K in keyof T]: ((input: T[K]) => void) | Promise<T[K]>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment