Skip to content

Commit

Permalink
types(schematypes): allow defining map path using type: 'Map'
Browse files Browse the repository at this point in the history
Fix #13755
  • Loading branch information
vkarpov15 committed Oct 10, 2023
1 parent 09e8445 commit c4416a6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
18 changes: 8 additions & 10 deletions test/types/maps.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Schema, model, Document, Model, Types } from 'mongoose';
import { expectType } from 'tsd';

interface ITest {
map1: Map<string, number>,
Expand Down Expand Up @@ -73,17 +74,14 @@ function gh10872(): void {
}

function gh13755() {
class Test {
instance: Map<string, string>;
constructor() {
this.instance = new Map<string, string>();
}
}

const testSchema = new Schema<Test>({
const testSchema = new Schema({
instance: {
type: 'Map',
of: 'Mixed'
of: String
}
});
} as const);

const TestModel = model('Test', testSchema);
const doc = new TestModel();
expectType<Map<string, string> | undefined>(doc.instance);
}
2 changes: 1 addition & 1 deletion test/types/schemaTypeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ expectType<BooleanSchemaDefinition | undefined>(new SchemaTypeOptions<boolean>()
expectType<NumberSchemaDefinition | undefined>(new SchemaTypeOptions<number>().type);
expectType<DateSchemaDefinition | undefined>(new SchemaTypeOptions<Date>().type);
expectType<StringSchemaDefinition | undefined>(new SchemaTypeOptions<string>().type);
expectType<SchemaDefinition<typeof Map> | SchemaDefinition<'Map'> | undefined>(new SchemaTypeOptions<Map<any, any>>().type);
expectType<SchemaDefinition<typeof Map> | undefined>(new SchemaTypeOptions<Map<any, any>>().type);
expectType<SchemaDefinition<typeof Buffer> | undefined>(new SchemaTypeOptions<Buffer>().type);
expectType<ObjectIdSchemaDefinition | undefined>(new SchemaTypeOptions<Types.ObjectId>().type);
expectType<AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId>> | undefined>(new SchemaTypeOptions<Types.ObjectId[]>().type);
Expand Down
2 changes: 1 addition & 1 deletion types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt ? bigint :
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 MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
Expand Down
2 changes: 1 addition & 1 deletion types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ declare module 'mongoose' {
T extends number ? NumberSchemaDefinition :
T extends boolean ? BooleanSchemaDefinition :
T extends NativeDate ? DateSchemaDefinition :
T extends Map<any, any> ? SchemaDefinition<typeof Map> | SchemaDefinition<'Map'> :
T extends Map<any, any> ? SchemaDefinition<typeof Map> :
T extends Buffer ? SchemaDefinition<typeof Buffer> :
T extends Types.ObjectId ? ObjectIdSchemaDefinition :
T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId, EnforcedDocType>> :
Expand Down

0 comments on commit c4416a6

Please sign in to comment.