不要怂,就是干,撸起袖子干!

Commit 98a40891 by Andrew Schmadel Committed by Sushant

fix(types): use correct `this` value in getterMethods and setterMethods (#11292)

1 parent dd428a06
Showing with 17 additions and 12 deletions
......@@ -1178,15 +1178,15 @@ export interface ModelNameOptions {
/**
* Interface for getterMethods in InitOptions
*/
export interface ModelGetterOptions {
[name: string]: (this: Model) => unknown;
export interface ModelGetterOptions<M extends Model = Model> {
[name: string]: (this: M) => unknown;
}
/**
* Interface for setterMethods in InitOptions
*/
export interface ModelSetterOptions {
[name: string]: (this: Model, val: any) => void;
export interface ModelSetterOptions<M extends Model = Model> {
[name: string]: (this: M, val: any) => void;
}
/**
......@@ -1477,12 +1477,12 @@ export interface ModelOptions<M extends Model = Model> {
/**
* Allows defining additional setters that will be available on model instances.
*/
setterMethods?: ModelSetterOptions;
setterMethods?: ModelSetterOptions<M>;
/**
* Allows defining additional getters that will be available on model instances.
*/
getterMethods?: ModelGetterOptions;
getterMethods?: ModelGetterOptions<M>;
/**
* Enable optimistic locking.
......@@ -1498,7 +1498,7 @@ export interface ModelOptions<M extends Model = Model> {
/**
* Options passed to [[Model.init]]
*/
export interface InitOptions extends ModelOptions {
export interface InitOptions<M extends Model = Model> extends ModelOptions<M> {
/**
* The sequelize connection. Required ATM.
*/
......@@ -1588,7 +1588,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
* string or a type-description object, with the properties described below:
* @param options These options are merged with the default define options provided to the Sequelize constructor
*/
public static init(attributes: ModelAttributes, options: InitOptions): void;
public static init<M extends Model = Model>(this: ModelCtor<M>, attributes: ModelAttributes, options: InitOptions<M>): void;
/**
* Remove attribute from model definition
......
......@@ -38,11 +38,16 @@ MyModel.init({}, {
}
],
sequelize,
tableName: 'my_model'
tableName: 'my_model',
getterMethods: {
multiply: function() {
return this.num * 2;
}
}
});
/**
* Tests for findCreateFind() type.
* Tests for findCreateFind() type.
*/
class UserModel extends Model {}
......@@ -56,9 +61,9 @@ UserModel.init({
UserModel.findCreateFind({
where: {
username: "new user username"
},
},
defaults: {
beta_user: true
beta_user: true
}
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!