Skip to content

Commit

Permalink
fix: Improve type inference of paginate method
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Aug 29, 2021
1 parent 96f51b3 commit a1bef71
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
31 changes: 30 additions & 1 deletion __tests-tsd__/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ interface User {

(async () => {
expectType<IWithPagination<User[]>>(
await db('users').select('*').paginate<User[]>({
await db<User[]>('users').column('*').paginate({
perPage: 10,
currentPage: 1,
isFromStart: true,
isLengthAware: false,
})
);

expectType<IWithPagination<User[]>>(
await db('users').select<User[]>('*').paginate({
perPage: 10,
currentPage: 1,
isFromStart: true,
Expand Down Expand Up @@ -51,6 +60,26 @@ interface User {
).pagination
);

expectType<ILengthAwarePagination>(
(
await db<User[]>('users').select('*').paginate({
perPage: 10,
currentPage: 1,
isLengthAware: true,
})
).pagination
);

expectType<User[]>(
(
await db<User[]>('users').select('*').paginate({
perPage: 10,
currentPage: 1,
isLengthAware: true,
})
).data
);

expectType<IBasePagination>(
(
await db('users').select('*').paginate({
Expand Down
6 changes: 3 additions & 3 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ interface ILengthAwarePagination extends IBasePagination {

declare module 'knex' {
namespace Knex {
interface QueryBuilder {
paginate<TData = any[], TParams extends IPaginateParams = IPaginateParams>(
interface QueryBuilder<TRecord extends {} = any, TResult = any> {
paginate<TParams extends IPaginateParams = IPaginateParams>(
params: Readonly<TParams>
): Knex.QueryBuilder<any, IWithPagination<TData, TParams>>;
): Knex.QueryBuilder<TRecord, IWithPagination<TRecord, TParams>>;
}
}
}
Expand Down

0 comments on commit a1bef71

Please sign in to comment.