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

Typescript on pagination. #398

Open
Mirare23 opened this issue Mar 2, 2024 · 1 comment
Open

Typescript on pagination. #398

Mirare23 opened this issue Mar 2, 2024 · 1 comment
Labels
question Further information is requested

Comments

@Mirare23
Copy link

Mirare23 commented Mar 2, 2024

So im trying to get pagecount on responsed. Here's what i do

const { data: BlogResponses } = await useAsyncData("blog", () => find<IAttributes>("blogs", { populate: "*", sort: "id:desc", pagination: { page: 1, pageSize: 10, withCount: true }, fields: ["title", "description", "slug", "Date", "Is_recomended"], }) )

onMounted(() => { maxPage.value = BlogResponses.value?.meta.pagination.pageCount })

Im getting typescript error that pageCount is not exist. i checked the types on the modules it provide this

image

What i did wrong? im new to TS .

@Mirare23 Mirare23 added the question Further information is requested label Mar 2, 2024
@olrtg
Copy link
Contributor

olrtg commented Sep 30, 2024

Hey @Mirare23! hope you're doing great!

You cannot access properties that you don't know you're going to get, Typescript only knows that in both cases you're going to get total because is the common property between the two types. In order to tell TypeScript which one (the page or offset pagination object) you have in your response you have to "narrow" the type, to give you an example, in my project I do this:

<template>
  <div
    v-if="
      articles && // articles (or data) can be null so I'm checking that first
      'pageCount' in articles.meta.pagination && // here I'm checking for one of the properties of the `MetaResponsePaginationByPage` to tell TS I have that shape
      articles.meta.pagination.pageCount > 1 // now you can access the `MetaResponsePaginationByPage` properties without TS errors (all the child elements can access this shape too)
    "
    class="flex justify-end px-3 py-3.5 border-t border-gray-200 dark:border-gray-700"
  >
    <UPagination
      v-model="page"
      :page-count="articles.meta.pagination.pageSize"
      :total="articles.meta.pagination.total"
    />
  </div>
</template>

Hope it helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants