Last active
January 26, 2019 15:29
-
-
Save dderevjanik/50e6c2b08f6ad6c80a3f62a56f01d054 to your computer and use it in GitHub Desktop.
Mongo Typed ObjectID
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
| import { ObjectID } from "mongodb"; | |
| import { ObjectIDToString } from "./types"; | |
| interface PersonModel { | |
| _id?: ObjectID; | |
| created: number; | |
| lastLogin: number; | |
| profiles: Array<{ | |
| profileId: ObjectID; | |
| title: string; | |
| created: number; | |
| contentIDs: ObjectID[]; | |
| }>; | |
| } | |
| export type Person = ObjectIDToString<PersonModel>; |
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
| import { ObjectID } from "mongodb"; | |
| import { ObjectIDToString } from "./types"; | |
| type Convert1 = ObjectIDToString<number>; // $ExpectType number | |
| type Convert2 = ObjectIDToString<string>; // $ExpectType string | |
| type Convert3 = ObjectIDToString<never>; // $ExpectType never | |
| type Convert4 = ObjectIDToString<any>; // $ExpectType any | |
| type Convert5 = ObjectIDToString<number[]>; // $ExpectType number[] | |
| type Convert6 = ObjectIDToString<string[]>; // $ExpectType string[] | |
| type Convert7 = ObjectIDToString<ObjectID>; // $ExpectType string | |
| type Convert8 = ObjectIDToString<ObjectID[]>; // $ExpectType string[] | |
| // Advanced Test | |
| type Convert9 = ObjectIDToString<{ | |
| id: ObjectID; | |
| }>; | |
| type Convert10 = ObjectIDToString<{ | |
| id?: ObjectID; | |
| title: string; | |
| duration: number; | |
| contentIds: ObjectID[]; | |
| }>; | |
| type Convert11 = ObjectIDToString<{ | |
| _id: ObjectID; | |
| group: { | |
| groupId: ObjectID; | |
| contentIds: ObjectID[]; | |
| isPremium: number; | |
| owner: { | |
| ownerId: ObjectID; | |
| name: string; | |
| } | |
| } | |
| }> | |
| type Convert12 = ObjectIDToString<{ | |
| id?: ObjectID; | |
| profiles: [ | |
| { id: ObjectID; email: string; regDate: number; contentIds: ObjectID[]; }, | |
| { id: ObjectID; email: string; regDate: number; contentIds: ObjectID[]; } | |
| ] | |
| }>; | |
| type Convert13 = ObjectIDToString<{ | |
| id?: ObjectID; | |
| profiles: Array<{ | |
| id: ObjectID; | |
| email: string; | |
| regDate: number; | |
| contentIds: ObjectID[]; | |
| }>; | |
| }>; |
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
| import { ObjectID } from "mongodb"; | |
| export type ObjectIDToString<O> = { | |
| 0: string; | |
| 1: string[]; | |
| 2: O extends Array<infer A> ? ObjectIDToString<A>[] : never; | |
| 3: { | |
| [prop in keyof O]: ObjectIDToString<O[prop]> | |
| }; | |
| 4: O; | |
| 5: never; | |
| }[O extends ObjectID | |
| ? 0 | |
| : O extends ObjectID[] | |
| ? 1 | |
| : O extends Array<any> | |
| ? 2 | |
| : O extends { [prop: string]: any; } | |
| ? 3 | |
| : 4 | |
| ]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment