Skip to content

Commit 9128dbd

Browse files
Infer nested schema array to Types.DocumentArray instead of normal array.
1 parent a2ff573 commit 9128dbd

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

test/types/schema.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ export type AutoTypedSchemaType = {
540540
favoritDrink?: 'Tea' | 'Coffee',
541541
favoritColorMode: 'dark' | 'light'
542542
friendID?: Types.ObjectId;
543-
nestedArray: Array<{
543+
nestedArray: Types.DocumentArray<{
544544
date: Date;
545545
messages?: number;
546546
}>
@@ -661,7 +661,12 @@ function gh12030() {
661661
]
662662
});
663663

664-
// expectType<{ users: Types.DocumentArray<{ userName?: string, credit: number }> }>({} as InferSchemaType<typeof Schema3>);
664+
expectType<{
665+
users: Types.DocumentArray<{
666+
credit: number;
667+
username?: string;
668+
}>;
669+
}>({} as InferSchemaType<typeof Schema3>);
665670

666671

667672
const Schema4 = new Schema({
@@ -674,7 +679,7 @@ function gh12030() {
674679
data: { type: { role: Object }, default: {} }
675680
});
676681

677-
expectType<{ data: { role?: string } }>({} as InferSchemaType<typeof Schema5>);
682+
expectType<{ data: { role?: any } }>({} as InferSchemaType<typeof Schema5>);
678683

679684
const Schema6 = new Schema({
680685
track: {

types/inferschematype.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
171171
*/
172172
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
173173
PathValueType extends Schema ? InferSchemaType<PathValueType> :
174-
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ResolvePathType<Item>>[] :
174+
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any[], Item extends Schema ? Types.DocumentArray<ResolvePathType<Item>> : ResolvePathType<Item>[]> :
175175
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
176176
PathValueType extends NumberSchemaDefinition ? number :
177177
PathValueType extends DateSchemaDefinition ? Date :

0 commit comments

Comments
 (0)