Skip to content

Commit ac57deb

Browse files
committed
WIP: alternative changes to clean up type inference re: #14954
1 parent 6ec3b48 commit ac57deb

23 files changed

+154
-123
lines changed

test/types/aggregate.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Schema, model, Document, Expression, PipelineStage, Types, Model, Aggregate } from 'mongoose';
22
import { expectType } from 'tsd';
33

4-
const schema: Schema = new Schema({ name: { type: 'String' } });
4+
const schema = new Schema({ name: { type: 'String' } });
55

66
interface ITest {
77
name?: string;

test/types/collection.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Schema, model, Document, connection, Collection } from 'mongoose';
22

3-
const schema: Schema = new Schema({ name: { type: 'String' } });
3+
const schema = new Schema({ name: { type: 'String' } });
44

55
interface ITest {
66
name?: string;

test/types/connection.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ expectType<Connection>(createConnection('mongodb://127.0.0.1:27017/test', { appN
99

1010
const conn = createConnection();
1111

12-
expectAssignable<Model<{ name: string }, any, any, any>>(conn.model('Test', new Schema<{ name: string }>({ name: { type: String } })));
12+
expectAssignable<Model<{ name: string }, any, any, any>>(
13+
conn.model('Test', new Schema<unknown, { name: string }>({ name: { type: String } }))
14+
);
1315
expectType<Model<{ name: string }>>(conn.model<{ name: string }>('Test', new Schema({ name: { type: String } })));
1416

1517
expectType<Promise<Connection>>(conn.openUri('mongodb://127.0.0.1:27017/test'));
@@ -132,7 +134,7 @@ function schemaInstanceMethodsAndQueryHelpersOnConnection() {
132134
}
133135
type UserModel = Model<User, UserQueryHelpers, UserInstanceMethods> & UserStaticMethods;
134136

135-
const userSchema = new Schema<User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
137+
const userSchema = new Schema<unknown, User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
136138
name: String
137139
}, {
138140
statics: {

test/types/discriminator.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import mongoose, { Document, Model, Schema, SchemaDefinition, SchemaOptions, Types, model } from 'mongoose';
22

3-
const schema: Schema = new Schema({ name: { type: 'String' } });
3+
const schema = new Schema({ name: { type: 'String' } });
44

55
interface IBaseTest {
66
name?: string;
@@ -47,7 +47,7 @@ function test(): void {
4747

4848
const cardDbSchemaOptions: SchemaOptions = { discriminatorKey: 'type' };
4949

50-
const cardDbSchema: Schema = new Schema(
50+
const cardDbSchema = new Schema(
5151
cardDbBaseSchemaDefinition,
5252
cardDbSchemaOptions
5353
);
@@ -60,7 +60,7 @@ function test(): void {
6060

6161
const landDbAdditionalPropertiesSchemaDefinition: SchemaDefinition = {};
6262

63-
const landDbSchema: Schema = new Schema(
63+
const landDbSchema = new Schema(
6464
landDbAdditionalPropertiesSchemaDefinition
6565
);
6666

test/types/docArray.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function gh10293() {
77
arrayOfArray: string[][]; // <-- Array of Array
88
}
99

10-
const testSchema = new Schema<ITest>({
10+
const testSchema = new Schema<unknown, ITest>({
1111
name: {
1212
type: String,
1313
required: true
@@ -144,9 +144,9 @@ function gh14469() {
144144
};
145145
type UserModelType = Model<User, {}, UserDocumentProps>;
146146

147-
const userSchema = new Schema<User, UserModelType>(
147+
const userSchema = new Schema<unknown, User, UserModelType>(
148148
{
149-
names: [new Schema<Names>({ firstName: String })]
149+
names: [new Schema<unknown, Names>({ firstName: String })]
150150
},
151151
{ timestamps: true }
152152
);

test/types/lean.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function gh10345() {
3434
}
3535

3636
async function gh11761() {
37-
const thingSchema = new Schema<{ name: string }>({
37+
const thingSchema = new Schema<unknown, { name: string }>({
3838
name: Schema.Types.String
3939
});
4040

@@ -66,7 +66,7 @@ async function gh11118(): Promise<void> {
6666
avatar?: string;
6767
}
6868

69-
const schema = new Schema<User>({
69+
const schema = new Schema<unknown, User>({
7070
name: { type: String, required: true },
7171
email: { type: String, required: true },
7272
avatar: String
@@ -87,7 +87,7 @@ async function _11767() {
8787
answers: string[];
8888
correct: number;
8989
}
90-
const QuestionSchema = new Schema<Question>({
90+
const QuestionSchema = new Schema<unknown, Question>({
9191
text: String,
9292
answers: [String],
9393
correct: Number
@@ -97,7 +97,7 @@ async function _11767() {
9797
dateTaken: Date;
9898
questions: Question[];
9999
}
100-
const ExamSchema = new Schema<Exam>({
100+
const ExamSchema = new Schema<unknown, Exam>({
101101
element: String,
102102
dateTaken: Date,
103103
questions: [QuestionSchema]
@@ -218,7 +218,7 @@ async function gh15057() {
218218
value?: string;
219219
};
220220

221-
const TestSchema = new Schema<Attachment>({
221+
const TestSchema = new Schema<unknown, Attachment>({
222222
type: { type: String, required: true },
223223
value: { type: String }
224224
});
@@ -291,7 +291,7 @@ async function gh15122() {
291291
ParentInstance
292292
>;
293293

294-
const parentSchema = new Schema<IParent, ParentModelType>(
294+
const parentSchema = new Schema<unknown, IParent, ParentModelType>(
295295
{
296296
name: {
297297
type: String,
@@ -330,7 +330,7 @@ async function gh15158() {
330330
};
331331

332332
const createSomeModelAndDoSomething = async <T extends FooBar>() => {
333-
const TestSchema = new Schema<T>({
333+
const TestSchema = new Schema<unknown, T>({
334334
value: { type: String }
335335
});
336336

test/types/maps.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface ITest {
77
map3: Map<string, number>
88
}
99

10-
const schema: Schema = new Schema<ITest>({
10+
const schema = new Schema<unknown, ITest>({
1111
map1: {
1212
type: Map,
1313
of: Number
@@ -50,12 +50,12 @@ function gh10575() {
5050
property3: string;
5151
}
5252

53-
const BaseSchema: Schema<IBase> = new Schema({ prop1: String, prop2: String });
53+
const BaseSchema: Schema<unknown, IBase> = new Schema({ prop1: String, prop2: String });
5454

55-
const Model1Schema: Schema<IModel1> = BaseSchema.clone() as any;
55+
const Model1Schema: Schema<unknown, IModel1> = BaseSchema.clone() as any;
5656
Model1Schema.add({ property1: Number, property2: Number });
5757

58-
const Model2Schema: Schema<IModel2> = BaseSchema.clone() as any;
58+
const Model2Schema: Schema<unknown, IModel2> = BaseSchema.clone() as any;
5959
Model2Schema.add({ property3: String });
6060

6161
const Model1 = model<IModel1, Model<IModel1>>('m1', Model1Schema);

test/types/methods.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Schema, Model, connection } from 'mongoose';
1+
import { Schema, Model, connection, ObtainSchemaGeneric } from 'mongoose';
2+
import { expectType } from 'tsd';
23

34
interface ITest {
45
foo: string;
@@ -10,7 +11,7 @@ interface ITestMethods {
1011

1112
type ITestModel = Model<ITest, {}, ITestMethods>;
1213

13-
const TestSchema = new Schema<ITest, ITestModel, ITestMethods>({
14+
const TestSchema = new Schema<unknown, ITest, ITestModel, ITestMethods>({
1415
foo: { type: String, required: true }
1516
});
1617

@@ -26,3 +27,26 @@ Test.create({ foo: 'test' });
2627
const doc = new Test({ foo: 'test' });
2728

2829
Math.floor(doc.getAnswer());
30+
31+
function autoInferred() {
32+
const TestSchema = new Schema({
33+
foo: { type: String, required: true }
34+
}, {
35+
methods: {
36+
getAnswer() {
37+
console.log(this.foo.trim());
38+
return 42;
39+
}
40+
}
41+
});
42+
43+
const Test = connection.model<typeof TestSchema>('Test', TestSchema);
44+
type Methods = ObtainSchemaGeneric<typeof TestSchema, 'TInstanceMethods'>;
45+
type Hydrated = ObtainSchemaGeneric<typeof TestSchema, 'THydratedDocumentType'>;
46+
47+
Test.create({ foo: 'test' });
48+
49+
const doc = new Test({ foo: 'test' });
50+
51+
Math.floor(doc.getAnswer());
52+
}

test/types/middleware.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function gh11480(): void {
137137
name: string;
138138
};
139139

140-
const UserSchema = new Schema<IUserSchema>({ name: { type: String } });
140+
const UserSchema = new Schema<unknown, IUserSchema>({ name: { type: String } });
141141

142142
UserSchema.pre('save', function(next) {
143143
expectNotType<any>(this);
@@ -152,7 +152,7 @@ function gh12583() {
152152
avatar?: string;
153153
}
154154

155-
const userSchema = new Schema<IUser>({
155+
const userSchema = new Schema<unknown, IUser>({
156156
name: { type: String, required: true },
157157
email: { type: String, required: true },
158158
avatar: String
@@ -172,7 +172,7 @@ function gh11257() {
172172
avatar?: string;
173173
}
174174

175-
const schema = new Schema<User>({
175+
const schema = new Schema<unknown, User>({
176176
name: { type: String, required: true },
177177
email: { type: String, required: true },
178178
avatar: String
@@ -211,7 +211,7 @@ function gh15242() {
211211
type DocumentValidatorThis = HydratedDocument<PostPersisted>;
212212
type QueryValidatorThis = Query<unknown, PostRecord>;
213213

214-
const PostSchema = new Schema<PostPersisted>({
214+
const PostSchema = new Schema<unknown, PostPersisted>({
215215
title: { type: String, required: true },
216216
postTime: {
217217
type: Date,

test/types/plugin.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { connection, HydratedDocument, Model, Query, Schema } from 'mongoose';
22

3-
function pluginVirtuals(schema: Schema<Test, any, any, any, TestVirtuals>): void {
3+
function pluginVirtuals(schema: Schema<any, Test, any, any, any, TestVirtuals>): void {
44
schema.virtual('fullName').get(function(this: TestDocument) {
55
return `${this.firstName} ${this.lastName}`;
66
});
@@ -9,19 +9,19 @@ function pluginVirtuals(schema: Schema<Test, any, any, any, TestVirtuals>): void
99
});
1010
}
1111

12-
function pluginQueryHelpers(schema: Schema<Test, any, any, TestQueryHelpers>): void {
12+
function pluginQueryHelpers(schema: Schema<any, Test, any, any, TestQueryHelpers>): void {
1313
schema.query.whereSomething = function() {
1414
return this.where({ name: 'something' });
1515
};
1616
}
1717

18-
function pluginMethods(schema: Schema<Test, any, TestInstanceMethods>): void {
18+
function pluginMethods(schema: Schema<any, Test, any, TestInstanceMethods>): void {
1919
schema.methods.doSomething = function() {
2020
return 'test';
2121
};
2222
}
2323

24-
function pluginStatics(schema: Schema<Test, TestModel, any, TestQueryHelpers, any, TestStaticMethods>): void {
24+
function pluginStatics(schema: Schema<any, Test, TestModel, any, TestQueryHelpers, any, TestStaticMethods>): void {
2525
schema.statics.findSomething = function() {
2626
return this.findOne().orFail().exec();
2727
};
@@ -54,12 +54,12 @@ interface TestQueryHelpers {
5454
whereSomething(this: TestQuery): this
5555
}
5656
type TestModel = Model<Test, TestQueryHelpers, TestInstanceMethods, TestVirtuals> & TestStaticMethods;
57-
const testSchema = new Schema<Test, TestModel, TestInstanceMethods, TestQueryHelpers, TestVirtuals, TestStaticMethods>({
57+
const testSchema = new Schema<any, Test, TestModel, TestInstanceMethods, TestQueryHelpers, TestVirtuals, TestStaticMethods>({
5858
firstName: { type: String, required: true },
5959
lastName: { type: String, required: true }
6060
});
6161

62-
function pluginGeneric(schema: Schema): void {
62+
function pluginGeneric(schema: Schema<any>): void {
6363
schema.static('test', function() {
6464
return 0;
6565
});

0 commit comments

Comments
 (0)