Last active
July 25, 2017 12:31
-
-
Save benedyktdryl/2dced77e13b7ac99f605f643bbb65ea0 to your computer and use it in GitHub Desktop.
TypeScript tips & tricks from: http://ideasintosoftware.com/typescript-advanced-tricks/
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
// `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'>; |
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
// `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