Skip to content

Commit eb45389

Browse files
committed
add this context to statics + test
1 parent 1eb6e0c commit eb45389

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

test/types/schema.test.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ async function gh13797() {
12311231
} } });
12321232
}
12331233

1234-
async function gh14028() {
1234+
function gh14028_methods() {
12351235
// Methods that have access to `this` should have access to typing of other methods on the schema
12361236
interface IUser {
12371237
firstName: string;
@@ -1296,14 +1296,17 @@ async function gh14028() {
12961296
user2.fullName();
12971297
user2.isAdult();
12981298

1299+
type UserModelWithoutMethods = Model<IUser>;
12991300
// Skip InstanceMethods
1300-
const schema3 = new Schema<IUser, UserModel>({
1301+
const schema3 = new Schema<IUser, UserModelWithoutMethods>({
13011302
firstName: { type: String, required: true },
13021303
lastName: { type: String, required: true },
13031304
age: { type: Number, required: true }
13041305
}, {
13051306
methods: {
13061307
fullName() {
1308+
// Expect methods to still have access to `this` type
1309+
expectType<string>(this.firstName);
13071310
// As InstanceMethods type is not specified, expect type of this.fullName to be undefined
13081311
expectError<IUserMethods['fullName']>(this.fullName);
13091312
return this.firstName + ' ' + this.lastName;
@@ -1315,3 +1318,36 @@ async function gh14028() {
13151318
const user3 = new User3({ firstName: 'John', lastName: 'Doe', age: 20 });
13161319
expectError<string>(user3.fullName());
13171320
}
1321+
1322+
function gh14028_statics() {
1323+
// Methods that have access to `this` should have access to typing of other methods on the schema
1324+
interface IUser {
1325+
firstName: string;
1326+
lastName: string;
1327+
age: number;
1328+
}
1329+
interface IUserStatics {
1330+
createWithFullName(name: string): Promise<IUser>;
1331+
}
1332+
type UserModel = Model<IUser, {}>;
1333+
1334+
// Define statics on schema
1335+
const schema = new Schema<IUser, UserModel, {}, {}, {}, IUserStatics>({
1336+
firstName: { type: String, required: true },
1337+
lastName: { type: String, required: true },
1338+
age: { type: Number, required: true }
1339+
}, {
1340+
statics: {
1341+
createWithFullName(name: string) {
1342+
expectType<IUserStatics['createWithFullName']>(schema.statics.createWithFullName);
1343+
expectType<UserModel['create']>(this.create);
1344+
1345+
const [firstName, lastName] = name.split(' ');
1346+
return this.create({ firstName, lastName });
1347+
}
1348+
}
1349+
});
1350+
1351+
// Trigger type assertions inside statics
1352+
schema.statics.createWithFullName('John Doe');
1353+
}

types/schemaoptions.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ declare module 'mongoose' {
210210
TStaticMethods,
211211
{},
212212
{ [name: string]: (this: Model<DocType>, ...args: any[]) => unknown },
213-
{ [F in keyof TStaticMethods]: TStaticMethods[F] }
213+
AddThisParameter<TStaticMethods, Model<DocType>>
214214
>
215215

216216
/**

0 commit comments

Comments
 (0)