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): align the return value of increment and decrement with actual behavior #14704

Merged
merged 10 commits into from Jul 8, 2022
12 changes: 6 additions & 6 deletions src/model.d.ts
Expand Up @@ -2831,18 +2831,18 @@ export abstract class Model<TModelAttributes extends {} = any, TCreationAttribut
* value of `by` given in options. If an array is provided, the same is true for each column.
* If an object is provided, each key is incremented by the corresponding value, `by` is ignored.
*
* @returns an array of affected rows and affected count with `options.returning` true, whenever supported by dialect
* @returns an array of affected rows or with affected count if `options.returning` is true, whenever supported by dialect
*/
static increment<M extends Model>(
this: ModelStatic<M>,
fields: AllowReadonlyArray<keyof Attributes<M>>,
options: IncrementDecrementOptionsWithBy<Attributes<M>>
): Promise<[affectedRows: M[]]>;
): Promise<[affectedRows: M[], affectedCount?: number]>;
static increment<M extends Model>(
this: ModelStatic<M>,
fields: { [key in keyof Attributes<M>]?: number },
options: IncrementDecrementOptions<Attributes<M>>
): Promise<[affectedRows: M[]]>;
): Promise<[affectedRows: M[], affectedCount?: number]>;

/**
* Decrements the value of one or more attributes.
Expand All @@ -2853,20 +2853,20 @@ export abstract class Model<TModelAttributes extends {} = any, TCreationAttribut
* value of `by` given in options. If an array is provided, the same is true for each column.
* If an object is provided, each key is incremented by the corresponding value, `by` is ignored.
*
* @returns an array of affected rows and affected count with `options.returning` true, whenever supported by dialect
* @returns an array of affected rows or with affected count if `options.returning` is true, whenever supported by dialect
*
* @since 4.36.0
*/
static decrement<M extends Model>(
this: ModelStatic<M>,
fields: AllowReadonlyArray<keyof Attributes<M>>,
options: IncrementDecrementOptionsWithBy<Attributes<M>>
): Promise<M>;
): Promise<[affectedRows: M[], affectedCount?: number]>;
static decrement<M extends Model>(
this: ModelStatic<M>,
fields: { [key in keyof Attributes<M>]?: number },
options: IncrementDecrementOptions<Attributes<M>>
): Promise<M>;
): Promise<[affectedRows: M[], affectedCount?: number]>;

/**
* A hook that is run before validation
Expand Down
26 changes: 25 additions & 1 deletion test/types/model.ts
Expand Up @@ -87,6 +87,30 @@ MyModel.count({ group: 'type' }).then(result => {
expectTypeOf(result[0]).toMatchTypeOf<{ count: number }>();
});

MyModel.increment('int', { by: 1 }).then(result => {
expectTypeOf(result).toEqualTypeOf<[affectedRows: MyModel[], affectedCount?: number]>();
});

MyModel.increment({ int: 2 }, {}).then(result => {
expectTypeOf(result).toEqualTypeOf<[affectedRows: MyModel[], affectedCount?: number]>();
});

MyModel.increment(['int'], { by: 3 }).then(result => {
expectTypeOf(result).toEqualTypeOf<[affectedRows: MyModel[], affectedCount?: number]>();
});

MyModel.decrement('int', { by: 1 }).then(result => {
expectTypeOf(result).toEqualTypeOf<[affectedRows: MyModel[], affectedCount?: number]>();
});

MyModel.decrement({ int: 2 }, {}).then(result => {
expectTypeOf(result).toEqualTypeOf<[affectedRows: MyModel[], affectedCount?: number]>();
});

MyModel.decrement(['int'], { by: 3 }).then(result => {
expectTypeOf(result).toEqualTypeOf<[affectedRows: MyModel[], affectedCount?: number]>();
});

MyModel.build({ int: 10 }, { include: OtherModel });

MyModel.bulkCreate([{ int: 10 }], { include: OtherModel, searchPath: 'public' });
Expand All @@ -109,7 +133,7 @@ MyModel.update({}, { where: { str: 'bar' }, returning: ['str'] }).then(result =>

const sequelize = new Sequelize('mysql://user:user@localhost:3306/mydb');

const model: typeof MyModel = MyModel.init({
MyModel.init({
int: DataTypes.INTEGER,
str: DataTypes.STRING,
virtual: {
Expand Down