Skip to content

Commit

Permalink
[TypeScript] Add Object syntax overload to increment method (#5512)
Browse files Browse the repository at this point in the history
The [documentation for `QueryBuilder#icrements`](https://knexjs.org/guide/query-builder.html#increment) says, that the method accepts columns and values either as separate arguments, or in Object syntax ([the code also checks if the column is an object](https://github.com/knex/knex/blob/0d27bcb60b9bfd57fabf896715350089e0b1050c/lib/query/querybuilder.js#L1111)). 

But currently this code:

````typescript
knex("table")
    .where({ id: 1 })
    .increment({
      foo: 1,
      bar: 2,
    });
````

Throws an error: 

````
No overload matches this call.
  Overload 1 of 2, '(columnName: string | number | symbol, amount?: number): QueryBuilder<any, number>', gave the following error.
    Argument of type '{ views: number; clicks: number; sales: number; }' is not assignable to parameter of type 'string | number | symbol'.
  Overload 2 of 2, '(columnName: string, amount?: number): QueryBuilder<any, number>', gave the following error.
    Argument of type '{ views: number; clicks: number; sales: number; }' is not assignable to parameter of type 'string'.
````

This adds an overload to the increment method type definition for the case where the first argument is an Object consisting of keys from TRecord and numbers as values.
  • Loading branch information
lucakiebel committed Mar 28, 2023
1 parent 01688a4 commit c374a30
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,11 @@ export declare namespace Knex {
columnName: string,
amount?: number
): QueryBuilder<TRecord, number>;
increment(
columns: {
[column in keyof TRecord]: number
}
): QueryBuilder<TRecord, number>;

decrement(
columnName: keyof TRecord,
Expand Down

0 comments on commit c374a30

Please sign in to comment.