Skip to content

Commit

Permalink
fix: Improve type inference of paginate method (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh authored Aug 29, 2021
1 parent 96f51b3 commit 0ac3594
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
35 changes: 32 additions & 3 deletions __tests-tsd__/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { knex } from 'knex';
import { expectType } from 'tsd';
import { expectType, expectAssignable } from 'tsd';
import '../types';
import { ILengthAwarePagination, IBasePagination, IWithPagination } from '../types';

Expand All @@ -12,8 +12,17 @@ interface User {
}

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

expectAssignable<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 0ac3594

Please sign in to comment.