Skip to content

Commit

Permalink
Merge pull request #13628 from Automattic/vkarpov15/fix-ts-map-schema…
Browse files Browse the repository at this point in the history
…-type

types(schema): handle `type: Schema.Types.Map` in TypeScript
  • Loading branch information
vkarpov15 committed Jul 20, 2023
2 parents 49fa3ee + bee086f commit fc0f7c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
11 changes: 11 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,3 +1123,14 @@ function gh13534() {
const doc = new Test({ myId: '0'.repeat(24) });
expectType<Types.ObjectId>(doc.myId);
}

function maps() {
const schema = new Schema({
myMap: { type: Schema.Types.Map, of: Number, required: true }
});
const Test = model('Test', schema);

const doc = new Test({ myMap: { answer: 42 } });
expectType<Map<string, number>>(doc.myMap);
expectType<number | undefined>(doc.myMap!.get('answer'));
}
15 changes: 8 additions & 7 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,11 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
unknown;
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
unknown;

0 comments on commit fc0f7c1

Please sign in to comment.