Skip to content

Instantly share code, notes, and snippets.

@benedyktdryl
Last active July 25, 2017 12:31
Show Gist options
  • Save benedyktdryl/2dced77e13b7ac99f605f643bbb65ea0 to your computer and use it in GitHub Desktop.
Save benedyktdryl/2dced77e13b7ac99f605f643bbb65ea0 to your computer and use it in GitHub Desktop.
// `Omit` omit specific keys from existing interface
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]};
// `Omit` usage
type TCleanedUser = Omit<IUser, 'privateField1' | 'privateField2'>;
// `Projection` for typecheck your projection specifiers,
// i.e., which fields should - or should not - be returned in the result.
type Projection<T> = Partial<Record<keyof T, 0 | 1>>;
// `Projection` usage
MyCollection.find({}, {importantField: 1: anotherImportantField: 1} as Projection<IMyDoc>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment