Last active
December 30, 2022 14:15
-
-
Save gcavanunez/7e6ef78c99c4e9873353c07684a59b78 to your computer and use it in GitHub Desktop.
Typescript patterns
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
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