Skip to content

Commit b1215a8

Browse files
committed
fix raw type inference for schemas re: #14954
1 parent ac57deb commit b1215a8

File tree

6 files changed

+48
-43
lines changed

6 files changed

+48
-43
lines changed

test/types/document.test.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Drink = model('Drink', new Schema({
1919
name: String
2020
}));
2121

22-
const schema: Schema = new Schema({
22+
const schema = new Schema({
2323
name: { type: 'String', required: true },
2424
address: new Schema({ city: { type: String, required: true } }),
2525
favoritDrink: {
@@ -83,7 +83,7 @@ function testMethods(): void {
8383

8484
type User = Model<IUser, {}, IUserMethods>;
8585

86-
const schema = new Schema<IUser, User>({ first: String, last: String });
86+
const schema = new Schema<unknown, IUser, User>({ first: String, last: String });
8787
schema.methods.fullName = function(): string {
8888
return this.first + ' ' + this.last;
8989
};
@@ -100,7 +100,7 @@ function testRequiredId(): void {
100100
label: string;
101101
}
102102

103-
const FooSchema = new Schema<IFoo, Model<IFoo>, IFoo>({
103+
const FooSchema = new Schema<unknown, IFoo, Model<IFoo>, IFoo>({
104104
_id: String,
105105
label: { type: String }
106106
});
@@ -128,7 +128,7 @@ async function gh11117(): Promise<void> {
128128
someNumber: number;
129129
someString: string;
130130
}
131-
const fooSchema = new Schema<Foo, Model<Foo>>({
131+
const fooSchema = new Schema<unknown, Foo, Model<Foo>>({
132132
someDate: { required: true, type: Date },
133133
someId: { required: true, type: Schema.Types.ObjectId },
134134
someNumber: { required: true, type: Number },
@@ -155,7 +155,7 @@ function gh11085(): void {
155155
email: string;
156156
}
157157

158-
const userSchema = new Schema<User>({
158+
const userSchema = new Schema<unknown, User>({
159159
username: String,
160160
email: String
161161
});
@@ -173,7 +173,7 @@ function gh11435() {
173173
interface Item {
174174
name: string;
175175
}
176-
const ItemSchema = new Schema<Item>({ name: String });
176+
const ItemSchema = new Schema<unknown, Item>({ name: String });
177177

178178
ItemSchema.pre('validate', function preValidate() {
179179
expectType<Model<unknown>>(this.$model('Item1'));
@@ -235,15 +235,16 @@ async function gh11960() {
235235
type ParentModelType = Model<Parent, {}, {}, {}, ParentDocument>;
236236

237237
const ParentSchema = new Schema<
238-
Parent,
239-
ParentModelType,
240-
{},
241-
{},
242-
{},
243-
{},
244-
DefaultSchemaOptions,
245-
Parent,
246-
ParentDocument
238+
unknown,
239+
Parent,
240+
ParentModelType,
241+
{},
242+
{},
243+
{},
244+
{},
245+
DefaultSchemaOptions,
246+
Parent,
247+
ParentDocument
247248
>({
248249
username: { type: String },
249250
map: { type: Map, of: String },
@@ -287,7 +288,7 @@ function gh12290() {
287288
name: string;
288289
age: number;
289290
}
290-
const schema = new Schema<IUser>({
291+
const schema = new Schema<unknown, IUser>({
291292
name: String,
292293
age: Number
293294
});
@@ -340,7 +341,7 @@ function gh13738() {
340341
}
341342
}
342343

343-
const schema = new Schema<IPerson>({
344+
const schema = new Schema<unknown, IPerson>({
344345
age: Number,
345346
dob: Date,
346347
settings: {
@@ -383,7 +384,7 @@ async function gh14876() {
383384
year: number;
384385
owner: Types.ObjectId;
385386
};
386-
const carSchema = new Schema<CarObjectInterface>({
387+
const carSchema = new Schema<unknown, CarObjectInterface>({
387388
make: { type: String, required: true },
388389
model: { type: String, required: true },
389390
year: { type: Number, required: true },
@@ -394,7 +395,7 @@ async function gh14876() {
394395
name: string;
395396
age: number;
396397
};
397-
const userSchema = new Schema<UserObjectInterface>({
398+
const userSchema = new Schema<unknown, UserObjectInterface>({
398399
name: String,
399400
age: Number
400401
});
@@ -429,7 +430,7 @@ async function gh15077() {
429430
state: 'on' | 'off';
430431
};
431432

432-
const fooSchema = new Schema<Foo>(
433+
const fooSchema = new Schema<unknown, Foo>(
433434
{
434435
state: {
435436
type: String,

test/types/lean.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ async function gh13345_2() {
167167
url: { required: true, type: String }
168168
});
169169

170+
type X = typeof imageSchema extends Schema<any, any> ? InferSchemaType<typeof imageSchema> : never;
171+
170172
const placeSchema = new Schema({
171173
images: { required: true, type: [imageSchema] }
172174
});
@@ -175,6 +177,7 @@ async function gh13345_2() {
175177

176178
const PlaceModel = model('Place', placeSchema);
177179

180+
const p = await PlaceModel.findOne();
178181
const place = await PlaceModel.findOne().lean().orFail().exec();
179182
expectAssignable<FlattenMaps<Place>>(place);
180183
expectType<Record<string, string>>(place.images[0].description);

test/types/middleware.preposttypes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const preMiddlewareFn: PreSaveMiddlewareFunction<Document> = function(next, opts
1414
}
1515
};
1616

17-
const schema: Schema<IDocument> = new Schema<IDocument>({ name: { type: 'String' } });
17+
const schema = new Schema<unknown, IDocument>({ name: { type: 'String' } });
1818

1919
/* The following tests were generated by test/model.middleware.preposttypes.test.js (with GEN_TSD hardcoded set to true) */
2020

test/types/models.test.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function rawDocSyntax(): void {
3333

3434
type TestModel = Model<ITest, {}, ITestMethods>;
3535

36-
const TestSchema = new Schema<ITest, TestModel>({
36+
const TestSchema = new Schema<unknown, ITest, TestModel>({
3737
foo: { type: String, required: true }
3838
});
3939

@@ -53,7 +53,7 @@ function tAndDocSyntax(): void {
5353
foo: string;
5454
}
5555

56-
const TestSchema = new Schema<ITest & Document>({
56+
const TestSchema = new Schema<unknown, ITest & Document>({
5757
foo: { type: String, required: true }
5858
});
5959

@@ -69,7 +69,7 @@ async function insertManyTest() {
6969
foo: string;
7070
}
7171

72-
const TestSchema = new Schema<ITest>({
72+
const TestSchema = new Schema<unknown, ITest>({
7373
foo: { type: String, required: true }
7474
});
7575

@@ -91,7 +91,7 @@ function gh13930() {
9191
foo: string;
9292
}
9393

94-
const TestSchema = new Schema<ITest>({
94+
const TestSchema = new Schema<unknown, ITest>({
9595
foo: { type: String, required: true }
9696
});
9797

@@ -109,7 +109,7 @@ function gh10074() {
109109

110110
type IDogDocument = IDog & Document;
111111

112-
const DogSchema = new Schema<IDogDocument>(
112+
const DogSchema = new Schema<unknown, IDogDocument>(
113113
{
114114
breed: { type: String },
115115
name: { type: String },
@@ -169,9 +169,10 @@ interface ProjectModel extends Model<IProject, {}, IProjectInstanceMethods> {
169169
}
170170

171171
const projectSchema = new Schema<
172-
IProject,
173-
ProjectModel,
174-
IProjectInstanceMethods
172+
unknown,
173+
IProject,
174+
ProjectModel,
175+
IProjectInstanceMethods
175176
>({ name: String });
176177

177178
projectSchema.pre('save', function() {
@@ -407,7 +408,7 @@ function gh11911() {
407408
name?: string;
408409
}
409410

410-
const animalSchema = new Schema<IAnimal>({
411+
const animalSchema = new Schema<unknown, IAnimal>({
411412
name: { type: String }
412413
});
413414

@@ -426,7 +427,7 @@ function gh12059() {
426427
name?: string;
427428
}
428429

429-
const animalSchema = new Schema<IAnimal>({
430+
const animalSchema = new Schema<unknown, IAnimal>({
430431
name: { type: String }
431432
});
432433

@@ -454,7 +455,7 @@ function schemaInstanceMethodsAndQueryHelpers() {
454455
}
455456
type UserModel = Model<User, UserQueryHelpers, UserInstanceMethods> & UserStaticMethods;
456457

457-
const userSchema = new Schema<User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
458+
const userSchema = new Schema<unknown, User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
458459
name: String
459460
}, {
460461
statics: {
@@ -508,7 +509,7 @@ function gh12100() {
508509
})();
509510

510511

511-
function modelRemoveOptions() {
512+
async function modelRemoveOptions() {
512513
const cmodel = model('Test', new Schema());
513514

514515
const res: DeleteResult = await cmodel.deleteOne({}, {});
@@ -518,7 +519,7 @@ async function gh12286() {
518519
interface IUser{
519520
name: string;
520521
}
521-
const schema = new Schema<IUser>({
522+
const schema = new Schema<unknown, IUser>({
522523
name: { type: String, required: true }
523524
});
524525

@@ -536,7 +537,7 @@ function gh12332() {
536537
interface IUser{
537538
age: number
538539
}
539-
const schema = new Schema<IUser>({ age: Number });
540+
const schema = new Schema<unknown, IUser>({ age: Number });
540541

541542
const User = model<IUser>('User', schema);
542543

@@ -548,7 +549,7 @@ async function gh12347() {
548549
interface IUser{
549550
name: string;
550551
}
551-
const schema = new Schema<IUser>({
552+
const schema = new Schema<unknown, IUser>({
552553
name: { type: String, required: true }
553554
});
554555

@@ -768,7 +769,7 @@ function gh13897() {
768769
updatedAt: Date;
769770
}
770771

771-
const documentSchema = new Schema<IDocument>({
772+
const documentSchema = new Schema<unknown, IDocument>({
772773
name: { type: String, required: true }
773774
},
774775
{
@@ -786,7 +787,7 @@ async function gh14026() {
786787
bar: string[];
787788
}
788789

789-
const FooModel = mongoose.model<Foo>('Foo', new mongoose.Schema<Foo>({ bar: [String] }));
790+
const FooModel = mongoose.model<Foo>('Foo', new Schema<unknown, Foo>({ bar: [String] }));
790791

791792
const distinctBar = await FooModel.distinct('bar');
792793
expectType<string[]>(distinctBar);
@@ -807,7 +808,7 @@ async function gh14072() {
807808
updated_at: number;
808809
};
809810

810-
const schema = new mongoose.Schema<Test>(
811+
const schema = new Schema<unknown, Test>(
811812
{
812813
num: { type: Number },
813814
created_at: { type: Number },

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ declare module 'mongoose' {
273273
* Create a new schema
274274
*/
275275
constructor(
276-
definition: TSchemaDefinition,
276+
definition?: TSchemaDefinition,
277277
options?: SchemaOptions<
278278
RawDocType,
279279
TInstanceMethods,

types/inferrawdoctype.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ declare module 'mongoose' {
4949
* @returns Number, "Number" or "number" will be resolved to number type.
5050
*/
5151
type ResolveRawPathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends string = DefaultSchemaOptions['typeKey']> =
52-
PathValueType extends Schema<any> ?
52+
PathValueType extends Schema<any, any> ?
5353
InferSchemaType<PathValueType> :
5454
PathValueType extends (infer Item)[] ?
55-
IfEquals<Item, never, any[], Item extends Schema<any> ?
55+
IfEquals<Item, never, any[], Item extends Schema<any, any> ?
5656
// If Item is a schema, infer its type.
5757
Array<InferSchemaType<Item>> :
5858
Item extends Record<TypeKey, any> ?
@@ -72,7 +72,7 @@ declare module 'mongoose' {
7272
ObtainRawDocumentPathType<Item, TypeKey>[]
7373
>:
7474
PathValueType extends ReadonlyArray<infer Item> ?
75-
IfEquals<Item, never, any[], Item extends Schema<any> ?
75+
IfEquals<Item, never, any[], Item extends Schema<any, any> ?
7676
Array<InferSchemaType<Item>> :
7777
Item extends Record<TypeKey, any> ?
7878
Item[TypeKey] extends Function | String ?

0 commit comments

Comments
 (0)