MatrixAI / js-pagination

Pagination library that supports Offset and Cursor based pagination
https://matrix.ai
Apache License 2.0
0 stars 0 forks source link

`cursor.Query` does not properly encapsulate types #10

Closed DrFacepalm closed 3 years ago

DrFacepalm commented 3 years ago
type QuerySeekLimit<S> = Readonly<{
  order: boolean;
  seek?: S;
  limit?: number;
}>;
type QuerySeekAfterBefore<S> = Readonly<{
  order: null;
  seekAfter: S;
  seekBefore: S;
}>;
type Query<S> = QuerySeekLimit<S> | QuerySeekAfterBefore<S>;

When trying to use this type, e.g.

function x (query: cursor.Query<number>) {
  console.log(query.limit)
}

query.limit does not exist in QuerySeekAfterBefore and so TypeScript does not allow this.

DrFacepalm commented 3 years ago

Solved. Note that if you do

if (typeof pageQuery.order === "boolean") {
  console.log(query.seek)
}

this will not work because typescript does not properly check the type, but:

if (pageQuery.order !== null) {
  console.log(query.seek)
}

will work properly.