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: Improve type inference of paginate method #20

Merged
merged 1 commit into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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