From 7fba6684c2ffb658d28bf66649ae203704791033 Mon Sep 17 00:00:00 2001 From: Ariel Barabas <810664+knoid@users.noreply.github.com> Date: Sun, 14 Jun 2020 00:45:29 -0300 Subject: [PATCH] fix(types): specified 'this' for getters and setters in fields (#12370) --- types/lib/model.d.ts | 12 ++++++------ types/test/model.ts | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/types/lib/model.d.ts b/types/lib/model.d.ts index 0f5ad3a5b7e4..5ec7f4d7e7d0 100644 --- a/types/lib/model.d.ts +++ b/types/lib/model.d.ts @@ -1265,7 +1265,7 @@ export interface ModelAttributeColumnReferencesOptions { /** * Column options for the model schema attributes */ -export interface ModelAttributeColumnOptions extends ColumnOptions { +export interface ModelAttributeColumnOptions extends ColumnOptions { /** * A string or a data type */ @@ -1346,23 +1346,23 @@ export interface ModelAttributeColumnOptions extends ColumnOptions { * Provide a custom getter for this column. Use `this.getDataValue(String)` to manipulate the underlying * values. */ - get?(): unknown; + get?(this: M): unknown; /** * Provide a custom setter for this column. Use `this.setDataValue(String, Value)` to manipulate the * underlying values. */ - set?(val: unknown): void; + set?(this: M, val: unknown): void; } /** * Interface for Attributes provided for a column */ -export interface ModelAttributes { +export interface ModelAttributes { /** * The description of a database column */ - [name: string]: DataType | ModelAttributeColumnOptions; + [name: string]: DataType | ModelAttributeColumnOptions; } /** @@ -1612,7 +1612,7 @@ export abstract class Model 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(this: ModelCtor, attributes: ModelAttributes, options: InitOptions): void; + public static init(this: ModelCtor, attributes: ModelAttributes, options: InitOptions): void; /** * Remove attribute from model definition diff --git a/types/test/model.ts b/types/test/model.ts index b3ca9fef8e54..b9f2f68ec989 100644 --- a/types/test/model.ts +++ b/types/test/model.ts @@ -29,7 +29,17 @@ MyModel.findOne({ include: ['OtherModelAlias'] }); const sequelize = new Sequelize('mysql://user:user@localhost:3306/mydb'); -MyModel.init({}, { +MyModel.init({ + virtual: { + type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['num']), + get() { + return this.getDataValue('num') + 2; + }, + set(value: number) { + this.setDataValue('num', value - 2); + } + } +}, { indexes: [ { fields: ['foo'], @@ -71,4 +81,4 @@ UserModel.findCreateFind({ * Test for primaryKeyAttributes. */ class TestModel extends Model {}; -TestModel.primaryKeyAttributes; \ No newline at end of file +TestModel.primaryKeyAttributes;