Skip to content

Commit

Permalink
fix(types): align the return value of increment and decrement with ac…
Browse files Browse the repository at this point in the history
…tual behavior (#14704)
  • Loading branch information
fzn0x committed Jul 8, 2022
1 parent 27ffbc8 commit 89dd2ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
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

0 comments on commit 89dd2ec

Please sign in to comment.