@@ -1230,3 +1230,124 @@ async function gh13797() {
1230
1230
expectType < IUser > ( this ) ; return '' ;
1231
1231
} } } ) ;
1232
1232
}
1233
+
1234
+ function gh14028_methods ( ) {
1235
+ // Methods that have access to `this` should have access to typing of other methods on the schema
1236
+ interface IUser {
1237
+ firstName : string ;
1238
+ lastName : string ;
1239
+ age : number ;
1240
+ }
1241
+ interface IUserMethods {
1242
+ fullName ( ) : string ;
1243
+ isAdult ( ) : boolean ;
1244
+ }
1245
+ type UserModel = Model < IUser , { } , IUserMethods > ;
1246
+
1247
+ // Define methods on schema
1248
+ const schema = new Schema < IUser , UserModel , IUserMethods > ( {
1249
+ firstName : { type : String , required : true } ,
1250
+ lastName : { type : String , required : true } ,
1251
+ age : { type : Number , required : true }
1252
+ } , {
1253
+ methods : {
1254
+ fullName ( ) {
1255
+ // Expect type of `this` to have fullName method
1256
+ expectType < IUserMethods [ 'fullName' ] > ( this . fullName ) ;
1257
+ return this . firstName + ' ' + this . lastName ;
1258
+ } ,
1259
+ isAdult ( ) {
1260
+ // Expect type of `this` to have isAdult method
1261
+ expectType < IUserMethods [ 'isAdult' ] > ( this . isAdult ) ;
1262
+ return this . age >= 18 ;
1263
+ }
1264
+ }
1265
+ } ) ;
1266
+
1267
+ const User = model ( 'User' , schema ) ;
1268
+ const user = new User ( { firstName : 'John' , lastName : 'Doe' , age : 20 } ) ;
1269
+ // Trigger type assertions inside methods
1270
+ user . fullName ( ) ;
1271
+ user . isAdult ( ) ;
1272
+
1273
+ // Expect type of methods to be inferred if accessed directly
1274
+ expectType < IUserMethods [ 'fullName' ] > ( schema . methods . fullName ) ;
1275
+ expectType < IUserMethods [ 'isAdult' ] > ( schema . methods . isAdult ) ;
1276
+
1277
+ // Define methods outside of schema
1278
+ const schema2 = new Schema < IUser , UserModel , IUserMethods > ( {
1279
+ firstName : { type : String , required : true } ,
1280
+ lastName : { type : String , required : true } ,
1281
+ age : { type : Number , required : true }
1282
+ } ) ;
1283
+
1284
+ schema2 . methods . fullName = function fullName ( ) {
1285
+ expectType < IUserMethods [ 'fullName' ] > ( this . fullName ) ;
1286
+ return this . firstName + ' ' + this . lastName ;
1287
+ } ;
1288
+
1289
+ schema2 . methods . isAdult = function isAdult ( ) {
1290
+ expectType < IUserMethods [ 'isAdult' ] > ( this . isAdult ) ;
1291
+ return true ;
1292
+ } ;
1293
+
1294
+ const User2 = model ( 'User2' , schema2 ) ;
1295
+ const user2 = new User2 ( { firstName : 'John' , lastName : 'Doe' , age : 20 } ) ;
1296
+ user2 . fullName ( ) ;
1297
+ user2 . isAdult ( ) ;
1298
+
1299
+ type UserModelWithoutMethods = Model < IUser > ;
1300
+ // Skip InstanceMethods
1301
+ const schema3 = new Schema < IUser , UserModelWithoutMethods > ( {
1302
+ firstName : { type : String , required : true } ,
1303
+ lastName : { type : String , required : true } ,
1304
+ age : { type : Number , required : true }
1305
+ } , {
1306
+ methods : {
1307
+ fullName ( ) {
1308
+ // Expect methods to still have access to `this` type
1309
+ expectType < string > ( this . firstName ) ;
1310
+ // As InstanceMethods type is not specified, expect type of this.fullName to be undefined
1311
+ expectError < IUserMethods [ 'fullName' ] > ( this . fullName ) ;
1312
+ return this . firstName + ' ' + this . lastName ;
1313
+ }
1314
+ }
1315
+ } ) ;
1316
+
1317
+ const User3 = model ( 'User2' , schema3 ) ;
1318
+ const user3 = new User3 ( { firstName : 'John' , lastName : 'Doe' , age : 20 } ) ;
1319
+ expectError < string > ( user3 . fullName ( ) ) ;
1320
+ }
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
+ }
0 commit comments