fix: fix the type of the argument of KeyedMutator
for populateCache
#2933
+14
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The Problem
I will show you the problem with this code sandbox.
This container uses code sandbox introduced in swr docs with some changes.
I just added TypeScript and changed like below.
In this codesandbox, there is an serious type error:
in the source code:
I referred to this article.
In this article, the example written in JavaScript uses bound
mutate
to makeaddToDo
return just one updated item, rather than multiple updated items.However, when used in TypeScript like the codesandbox I suggested, this results in a type error.
For instance, when useSWR fetches an API that returns a
ToDo[]
type, the first argument ofmutate
is expected to be of typeToDo[]
.This works fine in JavaScript, but the issue arises only when it's converted to TypeScript.
What this PR does
This PR fixes a type error problem related to
populateCache
and boundmutate
.Now, the first argument of bound
mutate
only accepts a type of data fetched byuseSWR
.But in order to make use of
populateCache
, the first argument ofmutate
should accept any type of API fetching result.So I changed the type of the first argument from
Data
(which is the type of datauseSWR
fetches) intoMutationData
(which is the type of the data returned by the mutator)In addition, I changed types in
infinite
since it had error when I changed the type ofKeyedMutator
.