From bb2ef25871a6e04a7a9f248b6c077c963f57dae9 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Tue, 28 Jun 2022 22:05:29 +0100 Subject: [PATCH 01/22] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8Ftypes:=20fix=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Correctly detect types for Schema[] and Schema in typeKey - Use correct ObjectId type Fixes #12002 --- types/inferschematype.d.ts | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index df2fd0ce728..6b57a6d8e4b 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -1,4 +1,4 @@ -import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose'; +import { Schema, Types, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose'; declare module 'mongoose' { /** @@ -23,7 +23,7 @@ declare module 'mongoose' { * // result * type UserType = {userName?: string} */ - type InferSchemaType = ObtainSchemaGeneric ; + type InferSchemaType = ObtainSchemaGeneric; /** * @summary Obtains schema Generic type by using generic alias. @@ -42,7 +42,7 @@ declare module 'mongoose' { TPathTypeKey: TPathTypeKey; DocType: DocType; }[alias] - : unknown; + : unknown; } /** * @summary Checks if a type is "Record" or "any". @@ -74,7 +74,9 @@ type RequiredPathBaseType = { required: true | [true, string | undefined] }; * @description It helps to check if a path is defined by TypeKey OR not. * @param {TypeKey} TypeKey A literal string refers to path type property key. */ -type PathWithTypePropertyBaseType = { [k in TypeKey]: any }; +type PathWithTypePropertyBaseType = { + [k in TypeKey]: T; +}; /** * @summary A Utility to obtain schema's required path keys. @@ -120,10 +122,14 @@ type OptionalPaths = { */ type ObtainDocumentPathType = PathValueType extends Schema ? InferSchemaType + : PathValueType extends PathWithTypePropertyBaseType> + ? InferSchemaType + : PathValueType extends PathWithTypePropertyBaseType[]> + ? InferSchemaType[] : ResolvePathType< - PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType, - PathValueType extends PathWithTypePropertyBaseType ? Omit : {} - >; + PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType, + PathValueType extends PathWithTypePropertyBaseType ? Omit : {} + >; /** * @param {T} T A generic refers to string path enums. @@ -138,14 +144,14 @@ type PathEnumOrString['enum']> = T extends ( * @returns Number, "Number" or "number" will be resolved to string type. */ type ResolvePathType = {}> = - PathValueType extends (infer Item)[] ? IfEquals>[] : + PathValueType extends (infer Item)[] ? ResolvePathType[] : PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : - PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Schema.Types.ObjectId : - PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Schema.Types.Decimal128 : + PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : + PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : PathValueType extends MapConstructor ? Map> : PathValueType extends ArrayConstructor ? any[] : PathValueType extends typeof Schema.Types.Mixed ? any: From 2e51745e3cd452867add54116ac6470fa45d1adf Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Tue, 28 Jun 2022 22:12:31 +0100 Subject: [PATCH 02/22] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8Ftypes:=20improve=20i?= =?UTF-8?q?nferred=20types=20for=20arrays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The default value for an array field is an empty array unless specifed to be undefined. This checks if the path explicitly sets the default property to `undefined` --- types/inferschematype.d.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 6b57a6d8e4b..e05fbda2fe5 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -10,8 +10,8 @@ declare module 'mongoose' { */ type ObtainDocumentType = IsItRecordAndNotAny extends true ? EnforcedDocType : { - [K in keyof (RequiredPaths & - OptionalPaths)]: ObtainDocumentPathType; + [K in keyof (RequiredPaths & + OptionalPaths)]: ObtainDocumentPathType; }; /** @@ -83,8 +83,10 @@ type PathWithTypePropertyBaseType = { * @param {T} T A generic refers to document definition. * @returns required paths keys of document definition. */ -type RequiredPathKeys = { - [K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals : never; +type RequiredPathKeys = { + [K in keyof T]: T[K] extends RequiredPathBaseType + ? IfEquals ? never : K, K> + : never; }[keyof T]; /** @@ -92,8 +94,8 @@ type RequiredPathKeys = { * @param {T} T A generic refers to document definition. * @returns a record contains required paths with the corresponding type. */ -type RequiredPaths = { - [K in RequiredPathKeys]: T[K]; +type RequiredPaths = { + [K in RequiredPathKeys]: T[K]; }; /** @@ -101,17 +103,21 @@ type RequiredPaths = { * @param {T} T A generic refers to document definition. * @returns optional paths keys of document definition. */ -type OptionalPathKeys = { +type OptionalPathKeys = { [K in keyof T]: T[K] extends RequiredPathBaseType ? never : K; }[keyof T]; +type OptionalArray = { + [k in TypeKey]: any[]; +} & { default: undefined }; + /** * @summary A Utility to obtain schema's optional paths. * @param {T} T A generic refers to document definition. * @returns a record contains optional paths with the corresponding type. */ -type OptionalPaths = { - [K in OptionalPathKeys]?: T[K]; +type OptionalPaths = { + [K in OptionalPathKeys]?: T[K]; }; /** From 73706cb094487e2eb2f778e00c44b1ff27dc86ed Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Tue, 28 Jun 2022 22:18:15 +0100 Subject: [PATCH 03/22] =?UTF-8?q?=E2=9C=85test:=20add=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For bb2ef25871a6e04a7a9f248b6c077c963f57dae9 --- test/types/schema.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 3bfa731eea6..9673d1ca3da 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -475,6 +475,17 @@ export function autoTypedSchema() { message: '{VALUE} is not supported' }, required: true + }, + friendID: { + type: Schema.Types.ObjectId + }, + nestedArray: { + type: [ + new Schema({ + date: { type: Date, required: true }, + messages: Number + }) + ] } }, { statics: { @@ -516,6 +527,11 @@ export type AutoTypedSchemaType = { }, favoritDrink?: 'Tea' | 'Coffee', favoritColorMode: 'dark' | 'light' + friendID: Types.ObjectId; + nestedArray: Array<{ + date: Date; + messages?: number; + }> } , statics: { staticFn: () => 'Returned from staticFn' From 6277c4f2e8ae14268468138aee9587cea94cc5b6 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Tue, 28 Jun 2022 22:20:54 +0100 Subject: [PATCH 04/22] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8Ftypes:=20correct=20f?= =?UTF-8?q?riendId=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/types/schema.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 9673d1ca3da..212c0ad9d4a 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -527,7 +527,7 @@ export type AutoTypedSchemaType = { }, favoritDrink?: 'Tea' | 'Coffee', favoritColorMode: 'dark' | 'light' - friendID: Types.ObjectId; + friendID?: Types.ObjectId; nestedArray: Array<{ date: Date; messages?: number; From 687d4dbd2eb0baf2a3c968549ecde215f12ca3c3 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Tue, 28 Jun 2022 22:58:29 +0100 Subject: [PATCH 05/22] =?UTF-8?q?=F0=9F=90=9Bfix:=20handle=20deeper=20sche?= =?UTF-8?q?ma=20arrays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/inferschematype.d.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index e05fbda2fe5..6f4cd128b90 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -130,9 +130,8 @@ type ObtainDocumentPathType = Pa ? InferSchemaType : PathValueType extends PathWithTypePropertyBaseType> ? InferSchemaType - : PathValueType extends PathWithTypePropertyBaseType[]> - ? InferSchemaType[] : ResolvePathType< + TypeKey, PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType, PathValueType extends PathWithTypePropertyBaseType ? Omit : {} >; @@ -149,8 +148,8 @@ type PathEnumOrString['enum']> = T extends ( * @param {Options} Options Document definition path options except path type. * @returns Number, "Number" or "number" will be resolved to string type. */ -type ResolvePathType = {}> = - PathValueType extends (infer Item)[] ? ResolvePathType[] : +type ResolvePathType = {}> = + PathValueType extends (infer Item)[] ? ObtainDocumentPathType[] : PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : @@ -158,7 +157,7 @@ type ResolvePathType> : + PathValueType extends MapConstructor ? Map> : PathValueType extends ArrayConstructor ? any[] : PathValueType extends typeof Schema.Types.Mixed ? any: IfEquals extends true ? any: From e3679a8919e75ffd7056ce3457d1658e1aa6bae6 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Wed, 29 Jun 2022 00:37:37 +0100 Subject: [PATCH 06/22] =?UTF-8?q?=F0=9F=90=9Bfix:=20corrrectly=20filter=20?= =?UTF-8?q?`default:=20undefined`=20aray?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/inferschematype.d.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 6f4cd128b90..8af390a42bf 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -42,7 +42,7 @@ declare module 'mongoose' { TPathTypeKey: TPathTypeKey; DocType: DocType; }[alias] - : unknown; + : unknown; } /** * @summary Checks if a type is "Record" or "any". @@ -84,9 +84,10 @@ type PathWithTypePropertyBaseType = { * @returns required paths keys of document definition. */ type RequiredPathKeys = { - [K in keyof T]: T[K] extends RequiredPathBaseType - ? IfEquals ? never : K, K> - : never; + [K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals + : T[K] extends PathWithTypePropertyBaseType + ? T[K] extends { default: undefined } ? never : K + : never; }[keyof T]; /** @@ -104,13 +105,12 @@ type RequiredPaths = { * @returns optional paths keys of document definition. */ type OptionalPathKeys = { - [K in keyof T]: T[K] extends RequiredPathBaseType ? never : K; + [K in keyof T]: T[K] extends RequiredPathBaseType ? never : + T[K] extends PathWithTypePropertyBaseType + ? T[K] extends { default: undefined } ? K : never + : K; }[keyof T]; -type OptionalArray = { - [k in TypeKey]: any[]; -} & { default: undefined }; - /** * @summary A Utility to obtain schema's optional paths. * @param {T} T A generic refers to document definition. @@ -159,8 +159,8 @@ type ResolvePathType> : PathValueType extends ArrayConstructor ? any[] : - PathValueType extends typeof Schema.Types.Mixed ? any: - IfEquals extends true ? any: - IfEquals extends true ? any: + PathValueType extends typeof Schema.Types.Mixed ? any : + IfEquals extends true ? any : + IfEquals extends true ? any : PathValueType extends typeof SchemaType ? PathValueType['prototype'] : unknown; From 79c9ce4d5e8462ed66eeb2b176e7a85fea0f6f4d Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Wed, 29 Jun 2022 13:02:17 +0100 Subject: [PATCH 07/22] =?UTF-8?q?=F0=9F=90=9Bfix:=20resolves=20type=20of?= =?UTF-8?q?=20nested=20paths=20that=20isn't=20a=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/inferschematype.d.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 8af390a42bf..5cc9dfa138f 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -1,4 +1,4 @@ -import { Schema, Types, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose'; +import { Schema, Types, InferSchemaType, ObtainDocumentType, SchemaDefinitionProperty, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose'; declare module 'mongoose' { /** @@ -149,18 +149,19 @@ type PathEnumOrString['enum']> = T extends ( * @returns Number, "Number" or "number" will be resolved to string type. */ type ResolvePathType = {}> = - PathValueType extends (infer Item)[] ? ObtainDocumentPathType[] : - PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : - PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : - PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : - PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : - PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : - PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : - PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : - PathValueType extends MapConstructor ? Map> : - PathValueType extends ArrayConstructor ? any[] : - PathValueType extends typeof Schema.Types.Mixed ? any : - IfEquals extends true ? any : - IfEquals extends true ? any : - PathValueType extends typeof SchemaType ? PathValueType['prototype'] : - unknown; + PathValueType extends (infer Item)[] ? IfEquals>[] : + PathValueType extends { [K: string]: SchemaDefinitionProperty } ? ObtainDocumentType : + PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : + PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : + PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : + PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : + PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : + PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : + PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : + PathValueType extends MapConstructor ? Map> : + PathValueType extends ArrayConstructor ? any[] : + PathValueType extends typeof Schema.Types.Mixed ? any : + IfEquals extends true ? any : + IfEquals extends true ? any : + PathValueType extends typeof SchemaType ? PathValueType['prototype'] : + unknown; From 7d280a2961f82f009ece31aa4ac719f5d92afdd1 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Wed, 29 Jun 2022 13:43:04 +0100 Subject: [PATCH 08/22] =?UTF-8?q?=F0=9F=9A=A8lint:=20fix=20indentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/inferschematype.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 5cc9dfa138f..be14368ae3d 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -42,7 +42,7 @@ declare module 'mongoose' { TPathTypeKey: TPathTypeKey; DocType: DocType; }[alias] - : unknown; + : unknown; } /** * @summary Checks if a type is "Record" or "any". @@ -129,11 +129,11 @@ type OptionalPaths = { type ObtainDocumentPathType = PathValueType extends Schema ? InferSchemaType : PathValueType extends PathWithTypePropertyBaseType> - ? InferSchemaType - : ResolvePathType< - TypeKey, - PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType, - PathValueType extends PathWithTypePropertyBaseType ? Omit : {} + ? InferSchemaType + : ResolvePathType< + TypeKey, + PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType, + PathValueType extends PathWithTypePropertyBaseType ? Omit : {} >; /** From 2f1d2d929ecd50ce427b9e2ecc31cd2f7654b0df Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Wed, 29 Jun 2022 21:39:06 +0100 Subject: [PATCH 09/22] =?UTF-8?q?=F0=9F=90=9Bfix:=20use=20correct=20type?= =?UTF-8?q?=20in=20test=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/types/schema.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 212c0ad9d4a..edb4253ad0d 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -377,9 +377,9 @@ export function autoTypedSchema() { mixed1?: any; mixed2?: any; mixed3?: any; - objectId1?: Schema.Types.ObjectId; - objectId2?: Schema.Types.ObjectId; - objectId3?: Schema.Types.ObjectId; + objectId1?: Types.ObjectId; + objectId2?: Types.ObjectId; + objectId3?: Types.ObjectId; customSchema?: Int8; map1?: Map; map2?: Map; @@ -388,9 +388,9 @@ export function autoTypedSchema() { array3?: any[]; array4?: any[]; array5?: any[]; - decimal1?: Schema.Types.Decimal128; - decimal2?: Schema.Types.Decimal128; - decimal3?: Schema.Types.Decimal128; + decimal1?: Types.Decimal128; + decimal2?: Types.Decimal128; + decimal3?: Types.Decimal128; }; const TestSchema = new Schema({ From 5ce27ac96a18ad86649250581065ef611d302ccd Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Wed, 29 Jun 2022 21:40:46 +0100 Subject: [PATCH 10/22] =?UTF-8?q?=E2=9C=85test:=20remove=20mixed=20schema?= =?UTF-8?q?=20type=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/types/schema.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index edb4253ad0d..b6dcd9312f3 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -374,8 +374,8 @@ export function autoTypedSchema() { boolean2?: boolean; boolean3?: boolean; boolean4?: boolean; - mixed1?: any; - mixed2?: any; + // mixed1?: any; + // mixed2?: any; mixed3?: any; objectId1?: Types.ObjectId; objectId2?: Types.ObjectId; @@ -414,8 +414,8 @@ export function autoTypedSchema() { boolean2: 'Boolean', boolean3: 'boolean', boolean4: Schema.Types.Boolean, - mixed1: Object, - mixed2: {}, + // mixed1: Object, + // mixed2: {}, mixed3: Schema.Types.Mixed, objectId1: Schema.Types.ObjectId, objectId2: 'ObjectId', From d31aa08d6755f037306419d35fbe937122c95f88 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Wed, 29 Jun 2022 22:16:03 +0100 Subject: [PATCH 11/22] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8Ftypes:=20allow=20`Mi?= =?UTF-8?q?xed`,=20`{}`=20and=20`ObjectConstructor`=20resolve=20to=20`unkn?= =?UTF-8?q?own`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/types/schema.test.ts | 8 ++++---- types/inferschematype.d.ts | 7 ++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index b6dcd9312f3..edb4253ad0d 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -374,8 +374,8 @@ export function autoTypedSchema() { boolean2?: boolean; boolean3?: boolean; boolean4?: boolean; - // mixed1?: any; - // mixed2?: any; + mixed1?: any; + mixed2?: any; mixed3?: any; objectId1?: Types.ObjectId; objectId2?: Types.ObjectId; @@ -414,8 +414,8 @@ export function autoTypedSchema() { boolean2: 'Boolean', boolean3: 'boolean', boolean4: Schema.Types.Boolean, - // mixed1: Object, - // mixed2: {}, + mixed1: Object, + mixed2: {}, mixed3: Schema.Types.Mixed, objectId1: Schema.Types.ObjectId, objectId2: 'ObjectId', diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index be14368ae3d..d084f8da00f 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -160,8 +160,5 @@ type ResolvePathType> : PathValueType extends ArrayConstructor ? any[] : - PathValueType extends typeof Schema.Types.Mixed ? any : - IfEquals extends true ? any : - IfEquals extends true ? any : - PathValueType extends typeof SchemaType ? PathValueType['prototype'] : - unknown; + PathValueType extends typeof SchemaType ? PathValueType['prototype'] : + unknown; From 4123249c9281289c0d83f2257ef00100a6c421f9 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Thu, 30 Jun 2022 00:20:04 +0100 Subject: [PATCH 12/22] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8Ftypes:=20use=20`unkn?= =?UTF-8?q?own`=20as=20expected=20mixed=20schema=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/types/schema.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index edb4253ad0d..0e640d85fd5 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -374,9 +374,9 @@ export function autoTypedSchema() { boolean2?: boolean; boolean3?: boolean; boolean4?: boolean; - mixed1?: any; - mixed2?: any; - mixed3?: any; + mixed1?: unknown; + mixed2?: unknown; + mixed3?: unknown; objectId1?: Types.ObjectId; objectId2?: Types.ObjectId; objectId3?: Types.ObjectId; From d12af9337f79637f737d7684b2694e0e734d03e4 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Thu, 30 Jun 2022 08:46:49 +0100 Subject: [PATCH 13/22] =?UTF-8?q?=F0=9F=90=9Bfix:=20reviewed=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/inferschematype.d.ts | 67 +++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index d084f8da00f..66f06750973 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -74,9 +74,15 @@ type RequiredPathBaseType = { required: true | [true, string | undefined] }; * @description It helps to check if a path is defined by TypeKey OR not. * @param {TypeKey} TypeKey A literal string refers to path type property key. */ -type PathWithTypePropertyBaseType = { - [k in TypeKey]: T; -}; +type PathWithTypePropertyBaseType = { [k in TypeKey]: any }; + +/** + * @summary + * @description Gets the type of a path defined by using TypeKey or directly specify type + * @param {PathValueType} PathValueType The schema path + * @param {TypeKey} TypeKey A literal string refers to path type property key. + */ +type PathTypeDefinition = PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType /** * @summary A Utility to obtain schema's required path keys. @@ -85,7 +91,9 @@ type PathWithTypePropertyBaseType = { */ type RequiredPathKeys = { [K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals - : T[K] extends PathWithTypePropertyBaseType + // Check if the path's type is an array + : PathTypeDefinition extends any[] + // If the path is an array and is defaulted to undefined, then we remove it from the required keys ? T[K] extends { default: undefined } ? never : K : never; }[keyof T]; @@ -96,8 +104,8 @@ type RequiredPathKeys = { * @returns a record contains required paths with the corresponding type. */ type RequiredPaths = { - [K in RequiredPathKeys]: T[K]; -}; + [K in RequiredPathKeys]: T[K] +} /** * @summary A Utility to obtain schema's optional path keys. @@ -105,8 +113,10 @@ type RequiredPaths = { * @returns optional paths keys of document definition. */ type OptionalPathKeys = { - [K in keyof T]: T[K] extends RequiredPathBaseType ? never : - T[K] extends PathWithTypePropertyBaseType + [K in keyof T]: T[K] extends RequiredPathBaseType ? never + // Check if the path's type is an array + : PathTypeDefinition extends any[] + // If the path is an array and is defaulted to undefined, then we add it to the optional keys ? T[K] extends { default: undefined } ? K : never : K; }[keyof T]; @@ -126,15 +136,11 @@ type OptionalPaths = { * @param {PathValueType} PathValueType Document definition path type. * @param {TypeKey} TypeKey A generic refers to document definition. */ -type ObtainDocumentPathType = PathValueType extends Schema - ? InferSchemaType - : PathValueType extends PathWithTypePropertyBaseType> - ? InferSchemaType - : ResolvePathType< - TypeKey, - PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType, - PathValueType extends PathWithTypePropertyBaseType ? Omit : {} - >; +type ObtainDocumentPathType = ResolvePathType< + TypeKey, + PathTypeDefinition, + PathValueType extends PathWithTypePropertyBaseType ? Omit : {} +>; /** * @param {T} T A generic refers to string path enums. @@ -149,16 +155,17 @@ type PathEnumOrString['enum']> = T extends ( * @returns Number, "Number" or "number" will be resolved to string type. */ type ResolvePathType = {}> = - PathValueType extends (infer Item)[] ? IfEquals>[] : - PathValueType extends { [K: string]: SchemaDefinitionProperty } ? ObtainDocumentType : - PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : - PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : - PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : - PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : - PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : - PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : - PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : - PathValueType extends MapConstructor ? Map> : - PathValueType extends ArrayConstructor ? any[] : - PathValueType extends typeof SchemaType ? PathValueType['prototype'] : - unknown; + PathValueType extends Schema ? InferSchemaType : + PathValueType extends (infer Item)[] ? IfEquals>[] : + PathValueType extends { [K: string]: SchemaDefinitionProperty } ? ObtainDocumentType : + PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : + PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : + PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : + PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : + PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : + PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : + PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : + PathValueType extends MapConstructor ? Map> : + PathValueType extends ArrayConstructor ? any[] : + PathValueType extends typeof SchemaType ? PathValueType['prototype'] : + any; From 4037696dba4b6c24d867f0cda0f2e111c231d98b Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Thu, 30 Jun 2022 09:09:28 +0100 Subject: [PATCH 14/22] Restore schema.test.ts --- test/types/schema.test.ts | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 0e640d85fd5..3bfa731eea6 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -374,12 +374,12 @@ export function autoTypedSchema() { boolean2?: boolean; boolean3?: boolean; boolean4?: boolean; - mixed1?: unknown; - mixed2?: unknown; - mixed3?: unknown; - objectId1?: Types.ObjectId; - objectId2?: Types.ObjectId; - objectId3?: Types.ObjectId; + mixed1?: any; + mixed2?: any; + mixed3?: any; + objectId1?: Schema.Types.ObjectId; + objectId2?: Schema.Types.ObjectId; + objectId3?: Schema.Types.ObjectId; customSchema?: Int8; map1?: Map; map2?: Map; @@ -388,9 +388,9 @@ export function autoTypedSchema() { array3?: any[]; array4?: any[]; array5?: any[]; - decimal1?: Types.Decimal128; - decimal2?: Types.Decimal128; - decimal3?: Types.Decimal128; + decimal1?: Schema.Types.Decimal128; + decimal2?: Schema.Types.Decimal128; + decimal3?: Schema.Types.Decimal128; }; const TestSchema = new Schema({ @@ -475,17 +475,6 @@ export function autoTypedSchema() { message: '{VALUE} is not supported' }, required: true - }, - friendID: { - type: Schema.Types.ObjectId - }, - nestedArray: { - type: [ - new Schema({ - date: { type: Date, required: true }, - messages: Number - }) - ] } }, { statics: { @@ -527,11 +516,6 @@ export type AutoTypedSchemaType = { }, favoritDrink?: 'Tea' | 'Coffee', favoritColorMode: 'dark' | 'light' - friendID?: Types.ObjectId; - nestedArray: Array<{ - date: Date; - messages?: number; - }> } , statics: { staticFn: () => 'Returned from staticFn' From 3def8c32171f7c44ea600cdcf4313316fb5ca142 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Thu, 30 Jun 2022 09:29:24 +0100 Subject: [PATCH 15/22] Revert "Restore schema.test.ts" This commit reverts 4037696dba4b6c24d867f0cda0f2e111c231d98b --- test/types/schema.test.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 3bfa731eea6..edb4253ad0d 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -377,9 +377,9 @@ export function autoTypedSchema() { mixed1?: any; mixed2?: any; mixed3?: any; - objectId1?: Schema.Types.ObjectId; - objectId2?: Schema.Types.ObjectId; - objectId3?: Schema.Types.ObjectId; + objectId1?: Types.ObjectId; + objectId2?: Types.ObjectId; + objectId3?: Types.ObjectId; customSchema?: Int8; map1?: Map; map2?: Map; @@ -388,9 +388,9 @@ export function autoTypedSchema() { array3?: any[]; array4?: any[]; array5?: any[]; - decimal1?: Schema.Types.Decimal128; - decimal2?: Schema.Types.Decimal128; - decimal3?: Schema.Types.Decimal128; + decimal1?: Types.Decimal128; + decimal2?: Types.Decimal128; + decimal3?: Types.Decimal128; }; const TestSchema = new Schema({ @@ -475,6 +475,17 @@ export function autoTypedSchema() { message: '{VALUE} is not supported' }, required: true + }, + friendID: { + type: Schema.Types.ObjectId + }, + nestedArray: { + type: [ + new Schema({ + date: { type: Date, required: true }, + messages: Number + }) + ] } }, { statics: { @@ -516,6 +527,11 @@ export type AutoTypedSchemaType = { }, favoritDrink?: 'Tea' | 'Coffee', favoritColorMode: 'dark' | 'light' + friendID?: Types.ObjectId; + nestedArray: Array<{ + date: Date; + messages?: number; + }> } , statics: { staticFn: () => 'Returned from staticFn' From 27614de38ce1765aa4b981dfb0f89ab9efd33877 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Thu, 30 Jun 2022 11:01:28 +0100 Subject: [PATCH 16/22] =?UTF-8?q?=F0=9F=9A=A8lint:=20fix=20warnings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/inferschematype.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 66f06750973..02384fbdb2d 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -82,7 +82,7 @@ type PathWithTypePropertyBaseType = { [k in Typ * @param {PathValueType} PathValueType The schema path * @param {TypeKey} TypeKey A literal string refers to path type property key. */ -type PathTypeDefinition = PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType +type PathTypeDefinition = PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType; /** * @summary A Utility to obtain schema's required path keys. @@ -104,8 +104,8 @@ type RequiredPathKeys = { * @returns a record contains required paths with the corresponding type. */ type RequiredPaths = { - [K in RequiredPathKeys]: T[K] -} + [K in RequiredPathKeys]: T[K]; +}; /** * @summary A Utility to obtain schema's optional path keys. @@ -137,9 +137,9 @@ type OptionalPaths = { * @param {TypeKey} TypeKey A generic refers to document definition. */ type ObtainDocumentPathType = ResolvePathType< - TypeKey, - PathTypeDefinition, - PathValueType extends PathWithTypePropertyBaseType ? Omit : {} +TypeKey, +PathTypeDefinition, +PathValueType extends PathWithTypePropertyBaseType ? Omit : {} >; /** From 1abdb3447eb7d1dba7ed64664fe3e6f75c825c90 Mon Sep 17 00:00:00 2001 From: mohammad0-0ahmad Date: Thu, 30 Jun 2022 17:44:07 +0200 Subject: [PATCH 17/22] Fix TS errors And refactor inferschematype.d.ts file --- test/types/schema.test.ts | 13 +++--- types/inferschematype.d.ts | 90 +++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 55 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index edb4253ad0d..d8fe77c6ece 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -2,7 +2,6 @@ import { Schema, Document, SchemaDefinition, - SchemaDefinitionProperty, SchemaTypeOptions, Model, Types, @@ -383,11 +382,12 @@ export function autoTypedSchema() { customSchema?: Int8; map1?: Map; map2?: Map; - array1?: string[]; - array2?: any[]; - array3?: any[]; - array4?: any[]; - array5?: any[]; + array1: string[]; + array2: any[]; + array3: any[]; + array4: any[]; + array5: any[]; + array6: string[]; decimal1?: Types.Decimal128; decimal2?: Types.Decimal128; decimal3?: Types.Decimal128; @@ -428,6 +428,7 @@ export function autoTypedSchema() { array3: [Schema.Types.Mixed], array4: [{}], array5: [], + array6: { type: [String] }, decimal1: Schema.Types.Decimal128, decimal2: 'Decimal128', decimal3: 'decimal128' diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 02384fbdb2d..255f96f1437 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -1,4 +1,4 @@ -import { Schema, Types, InferSchemaType, ObtainDocumentType, SchemaDefinitionProperty, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose'; +import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, ObtainSchemaGeneric } from 'mongoose'; declare module 'mongoose' { /** @@ -6,13 +6,13 @@ declare module 'mongoose' { * @description Obtains document schema type from document Definition OR returns enforced schema type if it's provided. * @param {DocDefinition} DocDefinition A generic equals to the type of document definition "provided in as first parameter in Schema constructor". * @param {EnforcedDocType} EnforcedDocType A generic type enforced by user "provided before schema constructor". - * @param {TypeKey} TypeKey A generic of literal string type. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". */ - type ObtainDocumentType = - IsItRecordAndNotAny extends true ? EnforcedDocType : { - [K in keyof (RequiredPaths & - OptionalPaths)]: ObtainDocumentPathType; - }; + type ObtainDocumentType = + IsItRecordAndNotAny extends true ? EnforcedDocType : { + [K in keyof (RequiredPaths & + OptionalPaths)]: ObtainDocumentPathType; + }; /** * @summary Obtains document schema type from Schema instance. @@ -23,7 +23,7 @@ declare module 'mongoose' { * // result * type UserType = {userName?: string} */ - type InferSchemaType = ObtainSchemaGeneric; + type InferSchemaType = ObtainSchemaGeneric ; /** * @summary Obtains schema Generic type by using generic alias. @@ -66,8 +66,10 @@ type IfEquals = /** * @summary Required path base type. * @description It helps to check whereas if a path is required OR optional. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". + */ -type RequiredPathBaseType = { required: true | [true, string | undefined] }; +type RequiredPathBaseType = { required: true | [true, string | undefined] }| ArrayConstructor | any[] | Record; /** * @summary Path base type defined by using TypeKey @@ -76,31 +78,20 @@ type RequiredPathBaseType = { required: true | [true, string | undefined] }; */ type PathWithTypePropertyBaseType = { [k in TypeKey]: any }; -/** - * @summary - * @description Gets the type of a path defined by using TypeKey or directly specify type - * @param {PathValueType} PathValueType The schema path - * @param {TypeKey} TypeKey A literal string refers to path type property key. - */ -type PathTypeDefinition = PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType; - /** * @summary A Utility to obtain schema's required path keys. * @param {T} T A generic refers to document definition. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". * @returns required paths keys of document definition. */ type RequiredPathKeys = { - [K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals - // Check if the path's type is an array - : PathTypeDefinition extends any[] - // If the path is an array and is defaulted to undefined, then we remove it from the required keys - ? T[K] extends { default: undefined } ? never : K - : never; + [K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals : never; }[keyof T]; /** * @summary A Utility to obtain schema's required paths. * @param {T} T A generic refers to document definition. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". * @returns a record contains required paths with the corresponding type. */ type RequiredPaths = { @@ -110,20 +101,17 @@ type RequiredPaths = { /** * @summary A Utility to obtain schema's optional path keys. * @param {T} T A generic refers to document definition. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". * @returns optional paths keys of document definition. */ type OptionalPathKeys = { - [K in keyof T]: T[K] extends RequiredPathBaseType ? never - // Check if the path's type is an array - : PathTypeDefinition extends any[] - // If the path is an array and is defaulted to undefined, then we add it to the optional keys - ? T[K] extends { default: undefined } ? K : never - : K; + [K in keyof T]: T[K] extends RequiredPathBaseType ? never : K; }[keyof T]; /** * @summary A Utility to obtain schema's optional paths. * @param {T} T A generic refers to document definition. + * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". * @returns a record contains optional paths with the corresponding type. */ type OptionalPaths = { @@ -136,11 +124,12 @@ type OptionalPaths = { * @param {PathValueType} PathValueType Document definition path type. * @param {TypeKey} TypeKey A generic refers to document definition. */ -type ObtainDocumentPathType = ResolvePathType< -TypeKey, -PathTypeDefinition, -PathValueType extends PathWithTypePropertyBaseType ? Omit : {} ->; +type ObtainDocumentPathType = PathValueType extends Schema + ? InferSchemaType + : ResolvePathType< + PathValueType extends PathWithTypePropertyBaseType ? PathValueType[TypeKey] : PathValueType, + PathValueType extends PathWithTypePropertyBaseType ? Omit : {} + >; /** * @param {T} T A generic refers to string path enums. @@ -154,18 +143,21 @@ type PathEnumOrString['enum']> = T extends ( * @param {Options} Options Document definition path options except path type. * @returns Number, "Number" or "number" will be resolved to string type. */ -type ResolvePathType = {}> = - PathValueType extends Schema ? InferSchemaType : - PathValueType extends (infer Item)[] ? IfEquals>[] : - PathValueType extends { [K: string]: SchemaDefinitionProperty } ? ObtainDocumentType : - PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : - PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : - PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : - PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : - PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : - PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : - PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : - PathValueType extends MapConstructor ? Map> : - PathValueType extends ArrayConstructor ? any[] : - PathValueType extends typeof SchemaType ? PathValueType['prototype'] : - any; +type ResolvePathType = {}> = + PathValueType extends Schema ? InferSchemaType : + PathValueType extends (infer Item)[] ? IfEquals>[] : + PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : + PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : + PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : + PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : + PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : + PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : + PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : + PathValueType extends MapConstructor ? Map> : + PathValueType extends ArrayConstructor ? any[] : + PathValueType extends typeof Schema.Types.Mixed ? any: + IfEquals extends true ? any: + IfEquals extends true ? any: + PathValueType extends typeof SchemaType ? PathValueType['prototype'] : + PathValueType extends {} ? PathValueType : + unknown; From 1c82c05226896e3806dd9ee9bd22f2c0dcbf9def Mon Sep 17 00:00:00 2001 From: mohammad0-0ahmad Date: Fri, 1 Jul 2022 00:34:05 +0200 Subject: [PATCH 18/22] Coverage for array path implicit type. --- test/types/schema.test.ts | 2 ++ types/inferschematype.d.ts | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index d8fe77c6ece..fcbb1e49c7d 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -388,6 +388,7 @@ export function autoTypedSchema() { array4: any[]; array5: any[]; array6: string[]; + array7?: string[]; decimal1?: Types.Decimal128; decimal2?: Types.Decimal128; decimal3?: Types.Decimal128; @@ -429,6 +430,7 @@ export function autoTypedSchema() { array4: [{}], array5: [], array6: { type: [String] }, + array7: { type: [String], default: undefined }, decimal1: Schema.Types.Decimal128, decimal2: 'Decimal128', decimal3: 'decimal128' diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 255f96f1437..d18b6a5bada 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -1,4 +1,4 @@ -import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, ObtainSchemaGeneric } from 'mongoose'; +import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types } from 'mongoose'; declare module 'mongoose' { /** @@ -64,12 +64,18 @@ type IfEquals = (() => G extends U ? 1 : 0) ? Y : N; /** - * @summary Required path base type. - * @description It helps to check whereas if a path is required OR optional. + * @summary Checks if a document path is required or optional. + * @param {P} P Document path. * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition". - */ -type RequiredPathBaseType = { required: true | [true, string | undefined] }| ArrayConstructor | any[] | Record; +type IsPathRequired = + P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[] + ? true + : P extends (Record) + ? P extends { default: undefined } + ? false + : true + : false; /** * @summary Path base type defined by using TypeKey @@ -85,7 +91,7 @@ type PathWithTypePropertyBaseType = { [k in Typ * @returns required paths keys of document definition. */ type RequiredPathKeys = { - [K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals : never; + [K in keyof T]: IsPathRequired extends true ? IfEquals : never; }[keyof T]; /** @@ -105,7 +111,7 @@ type RequiredPaths = { * @returns optional paths keys of document definition. */ type OptionalPathKeys = { - [K in keyof T]: T[K] extends RequiredPathBaseType ? never : K; + [K in keyof T]: IsPathRequired extends true ? never : K; }[keyof T]; /** From 814a0db02bf43d23b218b1b97ec3d111d844f48d Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Fri, 1 Jul 2022 01:23:04 +0100 Subject: [PATCH 19/22] =?UTF-8?q?=F0=9F=90=9B=20fix:=20make=20paths=20with?= =?UTF-8?q?=20default=20specified=20required?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/inferschematype.d.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index d18b6a5bada..b4898f3e1d0 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -1,4 +1,4 @@ -import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types } from 'mongoose'; +import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, SchemaDefinitionProperty } from 'mongoose'; declare module 'mongoose' { /** @@ -75,7 +75,11 @@ type IsPathRequired = ? P extends { default: undefined } ? false : true - : false; + : P extends PathWithTypePropertyBaseType + ? P extends { default: ResolvePathType } + ? true + : false + : false; /** * @summary Path base type defined by using TypeKey From dc5e52a228003d19ec542df97146e878cc034709 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Fri, 1 Jul 2022 01:24:13 +0100 Subject: [PATCH 20/22] =?UTF-8?q?=F0=9F=90=9B=20fix:=20limit=20allowed=20t?= =?UTF-8?q?ypes=20to=20be=20required=20with=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/inferschematype.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index b4898f3e1d0..b53f5dbb8b2 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -1,4 +1,4 @@ -import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, SchemaDefinitionProperty } from 'mongoose'; +import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, NumberSchemaDefinition, StringSchemaDefinition, BooleanSchemaDefinition, DateSchemaDefinition } from 'mongoose'; declare module 'mongoose' { /** @@ -75,7 +75,7 @@ type IsPathRequired = ? P extends { default: undefined } ? false : true - : P extends PathWithTypePropertyBaseType + : P extends (Record) ? P extends { default: ResolvePathType } ? true : false @@ -156,11 +156,11 @@ type PathEnumOrString['enum']> = T extends ( type ResolvePathType = {}> = PathValueType extends Schema ? InferSchemaType : PathValueType extends (infer Item)[] ? IfEquals>[] : - PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString : - PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : - PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : + PathValueType extends StringSchemaDefinition ? PathEnumOrString : + PathValueType extends NumberSchemaDefinition ? number : + PathValueType extends DateSchemaDefinition ? Date : PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : - PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : + PathValueType extends BooleanSchemaDefinition ? boolean : PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : PathValueType extends MapConstructor ? Map> : From 8aa7f53a0a693b10f15b718f10d0f50873b2be99 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Fri, 1 Jul 2022 01:27:12 +0100 Subject: [PATCH 21/22] =?UTF-8?q?=E2=9C=85=20test:=20add=20test=20for=20sc?= =?UTF-8?q?hema=20paths=20with=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/types/schema.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index fcbb1e49c7d..a145ec1da71 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -357,14 +357,17 @@ export function autoTypedSchema() { string2?: string; string3?: string; string4?: string; + string5: string; number1?: number; number2?: number; number3?: number; number4?: number; + number5: number; date1?: Date; date2?: Date; date3?: Date; date4?: Date; + date5: Date; buffer1?: Buffer; buffer2?: Buffer; buffer3?: Buffer; @@ -373,6 +376,7 @@ export function autoTypedSchema() { boolean2?: boolean; boolean3?: boolean; boolean4?: boolean; + boolean5: boolean; mixed1?: any; mixed2?: any; mixed3?: any; @@ -399,14 +403,17 @@ export function autoTypedSchema() { string2: 'String', string3: 'string', string4: Schema.Types.String, + string5: { type: String, default: "ABCD" }, number1: Number, number2: 'Number', number3: 'number', number4: Schema.Types.Number, + number5: { type: Number, default: 10 }, date1: Date, date2: 'Date', date3: 'date', date4: Schema.Types.Date, + date5: { type: Date , default: new Date() }, buffer1: Buffer, buffer2: 'Buffer', buffer3: 'buffer', @@ -415,6 +422,7 @@ export function autoTypedSchema() { boolean2: 'Boolean', boolean3: 'boolean', boolean4: Schema.Types.Boolean, + boolean5: { type: Boolean , default: true }, mixed1: Object, mixed2: {}, mixed3: Schema.Types.Mixed, From ca6f97b19a472f430a49fa2ec85ee71e9ca6c514 Mon Sep 17 00:00:00 2001 From: Ademola Adedeji Date: Fri, 1 Jul 2022 15:32:35 +0100 Subject: [PATCH 22/22] =?UTF-8?q?=F0=9F=9A=A8=20lint:=20fix=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/types/schema.test.ts | 6 +++--- types/inferschematype.d.ts | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index a145ec1da71..96b44f0d2c4 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -403,7 +403,7 @@ export function autoTypedSchema() { string2: 'String', string3: 'string', string4: Schema.Types.String, - string5: { type: String, default: "ABCD" }, + string5: { type: String, default: 'ABCD' }, number1: Number, number2: 'Number', number3: 'number', @@ -413,7 +413,7 @@ export function autoTypedSchema() { date2: 'Date', date3: 'date', date4: Schema.Types.Date, - date5: { type: Date , default: new Date() }, + date5: { type: Date, default: new Date() }, buffer1: Buffer, buffer2: 'Buffer', buffer3: 'buffer', @@ -422,7 +422,7 @@ export function autoTypedSchema() { boolean2: 'Boolean', boolean3: 'boolean', boolean4: Schema.Types.Boolean, - boolean5: { type: Boolean , default: true }, + boolean5: { type: Boolean, default: true }, mixed1: Object, mixed2: {}, mixed3: Schema.Types.Mixed, diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index b53f5dbb8b2..ce94e6221c0 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -75,11 +75,11 @@ type IsPathRequired = ? P extends { default: undefined } ? false : true - : P extends (Record) - ? P extends { default: ResolvePathType } - ? true - : false - : false; + : P extends (Record) + ? P extends { default: ResolvePathType } + ? true + : false + : false; /** * @summary Path base type defined by using TypeKey