Skip to content

Instantly share code, notes, and snippets.

@mendes5
Created May 31, 2021 02:22
Show Gist options
  • Select an option

  • Save mendes5/66fda23c210dc93ced58af0397d4db89 to your computer and use it in GitHub Desktop.

Select an option

Save mendes5/66fda23c210dc93ced58af0397d4db89 to your computer and use it in GitHub Desktop.
const assertFieldsPresent = <T extends Record<string, unknown>, U extends keyof T>(data: T, fields: U[]): T & Required<Pick<T, U>> => {
for (let field of fields) {
const value = data[field];
if (value === undefined || value === null) {
throw new Error(`[assertFields] Field "${field}" was not present`);
}
}
// @ts-ignore;
return data;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment