'paginationRange.length' is possibly 'undefined'.ts(18048)
I don't know how to get rid of this
error
'paginationRange.length' is possibly 'undefined'.ts(18048)
export type IPaginate = {
totalCount: number
pageSize: number
siblingCount: number
currentPage: number
onPageChange?: () => void
}
paginateHook.ts
👇
export const usePagination = ({totalCount, pageSize, siblingCount = 1, currentPage}: IPaginate) => {
// do something here
}
UsePaginate.tsx
👇
const paginationRange = usePagination({
currentPage,
totalCount,
siblingCount,
pageSize
})
if ( currentPage === 0 || paginationRange?.length < 2){ // 'paginationRange.length' is possibly 'undefined'.ts(18048)
return null
}
2 Replies
you're using the optional chaining operator (
?.
) to prevent an error like "cannot access property length of undefined", but I think you still can't compare undefined
to 2 with <
at least not in typescript
you should probably be explicit about what you want to do if paginationRange
is undefined, before you check if the length is less than 2. The compiler wants to know that it's safe to check the length of a variable that might be undefined, so it needs to be sure it isn'tThank you
i have resolved it