Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I seem to always be fighting TypeScript. Can anyone tell me what I'm doing wrong here?
Argument of type '(old: number) => void' is not assignable to parameter of type 'number'
interface IPagination {
pageNumber: number;
setPageNumber: (num: number) => void;
latestData: unknown;
Pagination.tsx
Updated to show for the Pagination component
const Pagination: React.FC<IPagination> = (props: IPagination) => {
const { pageNumber, setPageNumber, latestData } = props;
return (
<button
disabled={pageNumber === 1}
onClick={() => setPageNumber((old: number) => Math.max(old - 1, 1))}
{strings.PREVIOUS}
</button>
<span>{pageNumber}</span>
<button
disabled={!latestData || !latestData.next}
onClick={() =>
setPageNumber((old) =>
!latestData || !latestData.next ? old : old + 1
{strings.NEXT}
</button>
–
–
Assuming setPageNumber
is a state-setting function passed in from a useState
hook in the parent, the correct type for it is not (num: number) => void
, but rather React.Dispatch<React.SetStateAction<number>>
, which expands to (value: React.SetStateAction<number>) => void
.
React.SetStateAction<number>
, in turn, expands to number | ((prevState: number) => number)
. So the final type for setPageNumber
is
(value: number | ((prevState: number) => number)) => void
–
–