Skip to content

Instantly share code, notes, and snippets.

@SephReed
Created September 15, 2019 19:00
Show Gist options
  • Save SephReed/eeb62da1f3fabcce1e37b9364779563f to your computer and use it in GitHub Desktop.
Save SephReed/eeb62da1f3fabcce1e37b9364779563f to your computer and use it in GitHub Desktop.
How to make Typescript Hydration Typings / Function
// 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