Skip to content

Commit b94d2fd

Browse files
authored
Merge pull request #14430 from Automattic/vkarpov15/gh-14367
types: consistently infer array of objects in schema as a DocumentArray
2 parents 06067de + 343593a commit b94d2fd

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

test/types/docArray.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,37 @@ async function gh13424() {
9393
const doc = new TestModel();
9494
expectType<Types.ObjectId | undefined>(doc.subDocArray[0]._id);
9595
}
96+
97+
async function gh14367() {
98+
const UserSchema = new Schema(
99+
{
100+
reminders: {
101+
type: [
102+
{
103+
type: { type: Schema.Types.String },
104+
date: { type: Schema.Types.Date },
105+
toggle: { type: Schema.Types.Boolean },
106+
notified: { type: Schema.Types.Boolean }
107+
}
108+
],
109+
default: [
110+
{ type: 'vote', date: new Date(), toggle: false, notified: false },
111+
{ type: 'daily', date: new Date(), toggle: false, notified: false },
112+
{ type: 'drop', date: new Date(), toggle: false, notified: false },
113+
{ type: 'claim', date: new Date(), toggle: false, notified: false },
114+
{ type: 'work', date: new Date(), toggle: false, notified: false }
115+
]
116+
},
117+
avatar: {
118+
type: Schema.Types.String
119+
}
120+
},
121+
{ timestamps: true }
122+
);
123+
124+
type IUser = InferSchemaType<typeof UserSchema>;
125+
expectType<string | null | undefined>({} as IUser['reminders'][0]['type']);
126+
expectType<Date | null | undefined>({} as IUser['reminders'][0]['date']);
127+
expectType<boolean | null | undefined>({} as IUser['reminders'][0]['toggle']);
128+
expectType<string | null | undefined>({} as IUser['avatar']);
129+
}

test/types/schema.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1059,10 +1059,10 @@ function gh12882() {
10591059
});
10601060
type tArrType = InferSchemaType<typeof arrType>;
10611061
expectType<{
1062-
fooArray: {
1062+
fooArray: Types.DocumentArray<{
10631063
type: string;
10641064
foo: number;
1065-
}[]
1065+
}>
10661066
}>({} as tArrType);
10671067
// Readonly array of strings
10681068
const rArrString = new Schema({
@@ -1110,10 +1110,10 @@ function gh12882() {
11101110
});
11111111
type rTArrType = InferSchemaType<typeof rArrType>;
11121112
expectType<{
1113-
fooArray: {
1113+
fooArray: Types.DocumentArray<{
11141114
type: string;
11151115
foo: number;
1116-
}[]
1116+
}>
11171117
}>({} as rTArrType);
11181118
}
11191119

types/inferschematype.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
229229
ObtainDocumentPathType<Item, TypeKey>[] :
230230
// If the type key isn't callable, then this is an array of objects, in which case
231231
// we need to call ObtainDocumentType to correctly infer its type.
232-
ObtainDocumentType<Item, any, { typeKey: TypeKey }>[] :
232+
Types.DocumentArray<ObtainDocumentType<Item, any, { typeKey: TypeKey }>> :
233233
IsSchemaTypeFromBuiltinClass<Item> extends true ?
234234
ObtainDocumentPathType<Item, TypeKey>[] :
235235
IsItRecordAndNotAny<Item> extends true ?

0 commit comments

Comments
 (0)