Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): specified this for getters and setters in fields #11648

Merged
merged 1 commit into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions types/lib/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ export interface ModelAttributeColumnReferencesOptions {
/**
* Column options for the model schema attributes
*/
export interface ModelAttributeColumnOptions extends ColumnOptions {
export interface ModelAttributeColumnOptions<M extends Model = Model> extends ColumnOptions {
/**
* A string or a data type
*/
Expand Down Expand Up @@ -1341,23 +1341,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<M extends Model = Model> {
/**
* The description of a database column
*/
[name: string]: DataType | ModelAttributeColumnOptions;
[name: string]: DataType | ModelAttributeColumnOptions<M>;
}

/**
Expand Down Expand Up @@ -1607,7 +1607,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<M extends Model = Model>(this: ModelCtor<M>, attributes: ModelAttributes, options: InitOptions<M>): void;
public static init<M extends Model = Model>(this: ModelCtor<M>, attributes: ModelAttributes<M>, options: InitOptions<M>): void;

/**
* Remove attribute from model definition
Expand Down
14 changes: 12 additions & 2 deletions types/test/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ MyModel.bulkCreate([{ int: 10 }], { include: OtherModel });

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'],
Expand Down Expand Up @@ -90,4 +100,4 @@ UserModel.findCreateFind({
* Test for primaryKeyAttributes.
*/
class TestModel extends Model {};
TestModel.primaryKeyAttributes;
TestModel.primaryKeyAttributes;