Created
September 15, 2019 19:00
-
-
Save SephReed/eeb62da1f3fabcce1e37b9364779563f to your computer and use it in GitHub Desktop.
How to make Typescript Hydration Typings / Function
This file contains hidden or 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
// Typings | |
export type ReportRequest<OUTLINE> = {[key in keyof OUTLINE]?: true}; | |
export type Reporter<OUTLINE> = | |
(request: ReportRequest<OUTLINE>) => Required<Pick<OUTLINE, keyof typeof request>>; | |
// Example model | |
interface IModel { | |
name: string; | |
age: number; | |
bio: string; | |
} | |
// Example Reporter | |
const modelHydrateFn: Reporter<IModel> = (req) => ({ | |
...(req.name) && {name: "Seph"}, | |
...(req.age) && {age: 29}, | |
...(req.bio) && {bio: "A human"}, | |
}); | |
// Example Usage | |
const info = modelHydrateFn({name: true, bio: true}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment