From 9128dbd28f2e29c4c22a789dce0ab569a505e1ae Mon Sep 17 00:00:00 2001 From: mohammad0-0ahmad Date: Mon, 4 Jul 2022 02:51:44 +0200 Subject: [PATCH] Infer nested schema array to Types.DocumentArray instead of normal array. --- test/types/schema.test.ts | 11 ++++++++--- types/inferschematype.d.ts | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index f99251a641b..6843e12cfaa 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -540,7 +540,7 @@ export type AutoTypedSchemaType = { favoritDrink?: 'Tea' | 'Coffee', favoritColorMode: 'dark' | 'light' friendID?: Types.ObjectId; - nestedArray: Array<{ + nestedArray: Types.DocumentArray<{ date: Date; messages?: number; }> @@ -661,7 +661,12 @@ function gh12030() { ] }); - // expectType<{ users: Types.DocumentArray<{ userName?: string, credit: number }> }>({} as InferSchemaType); + expectType<{ + users: Types.DocumentArray<{ + credit: number; + username?: string; + }>; + }>({} as InferSchemaType); const Schema4 = new Schema({ @@ -674,7 +679,7 @@ function gh12030() { data: { type: { role: Object }, default: {} } }); - expectType<{ data: { role?: string } }>({} as InferSchemaType); + expectType<{ data: { role?: any } }>({} as InferSchemaType); const Schema6 = new Schema({ track: { diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 1cf0cf91bae..b32a97b9ef7 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -171,7 +171,7 @@ type PathEnumOrString['enum']> = T extends ( */ type ResolvePathType = {}, TypeKey extends TypeKeyBaseType = DefaultTypeKey> = PathValueType extends Schema ? InferSchemaType : - PathValueType extends (infer Item)[] ? IfEquals>[] : + PathValueType extends (infer Item)[] ? IfEquals> : ResolvePathType[]> : PathValueType extends StringSchemaDefinition ? PathEnumOrString : PathValueType extends NumberSchemaDefinition ? number : PathValueType extends DateSchemaDefinition ? Date :