From 0ee889391bb82fe117d1209c75c38e444e068ddf Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 23 Nov 2022 16:08:21 -0500 Subject: [PATCH 1/2] fix(types): correctly infer ReadonlyArray types in schema definitions Fix #12611 --- test/types/schema.test.ts | 19 ++++++++++++++ types/inferschematype.d.ts | 53 +++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 7bd00a8d4b8..2a9f66b4d63 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -938,3 +938,22 @@ function gh12590() { }); } + +function gh12611() { + const reusableFields = { + description: { type: String, required: true }, + skills: { type: [Schema.Types.ObjectId], ref: "Skill", default: [] } + } as const; + + const firstSchema = new Schema({ + ...reusableFields, + anotherField: String + }); + + type Props = InferSchemaType; + expectType<{ + description: string; + skills: Types.ObjectId[]; + anotherField?: string; + }>({} as Props); +} diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index e8c345fc707..c4dc62f7502 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -161,29 +161,30 @@ type PathEnumOrString['enum']> = T extends ( type ResolvePathType = {}, TypeKey extends TypeKeyBaseType = DefaultTypeKey> = PathValueType extends Schema ? InferSchemaType : PathValueType extends (infer Item)[] ? IfEquals> : ObtainDocumentPathType[]> : - PathValueType extends StringSchemaDefinition ? PathEnumOrString : - IfEquals extends true ? PathEnumOrString : - IfEquals extends true ? PathEnumOrString : - PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray ? Options['enum'][number] : number : - IfEquals extends true ? number : - PathValueType extends DateSchemaDefinition ? Date : - IfEquals extends true ? Date : - PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : - PathValueType extends BooleanSchemaDefinition ? boolean : - IfEquals extends true ? boolean : - PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId : - IfEquals extends true ? Types.ObjectId : - IfEquals extends true ? Types.ObjectId : - PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : - IfEquals extends true ? Types.Decimal128 : - IfEquals extends true ? Types.Decimal128 : - PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer : - IfEquals extends true ? Buffer : - 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 Record ? ObtainDocumentType : - unknown; + PathValueType extends ReadonlyArray ? IfEquals> : ObtainDocumentPathType[]> : + PathValueType extends StringSchemaDefinition ? PathEnumOrString : + IfEquals extends true ? PathEnumOrString : + IfEquals extends true ? PathEnumOrString : + PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray ? Options['enum'][number] : number : + IfEquals extends true ? number : + PathValueType extends DateSchemaDefinition ? Date : + IfEquals extends true ? Date : + PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : + PathValueType extends BooleanSchemaDefinition ? boolean : + IfEquals extends true ? boolean : + PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId : + IfEquals extends true ? Types.ObjectId : + IfEquals extends true ? Types.ObjectId : + PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : + IfEquals extends true ? Types.Decimal128 : + IfEquals extends true ? Types.Decimal128 : + PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer : + IfEquals extends true ? Buffer : + 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 Record ? ObtainDocumentType : + unknown; From 12706ebdd0ea039103636ef9ea4d25f7a0282f00 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 23 Nov 2022 16:14:05 -0500 Subject: [PATCH 2/2] style: fix lint --- 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 2a9f66b4d63..a531247e183 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -942,14 +942,14 @@ function gh12590() { function gh12611() { const reusableFields = { description: { type: String, required: true }, - skills: { type: [Schema.Types.ObjectId], ref: "Skill", default: [] } + skills: { type: [Schema.Types.ObjectId], ref: 'Skill', default: [] } } as const; - + const firstSchema = new Schema({ ...reusableFields, anotherField: String }); - + type Props = InferSchemaType; expectType<{ description: string;