swr icon indicating copy to clipboard operation
swr copied to clipboard

fix: fix the type of the argument of `KeyedMutator` for `populateCache`

Open Key5n opened this issue 1 year ago • 4 comments

closes https://github.com/vercel/swr/issues/2975

What this PR does

This PR fixes a type error problem related to populateCache and bound mutate.

Now, the first argument of bound mutate only accepts the type of the returned value of fetcher by useSWR.

But in order to make use of populateCache, the first argument of mutate should accept any type of API fetching result.

So I changed the type of the first argument from Data (which is the type of data useSWR fetches) into MutationData (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 of KeyedMutator .

Key5n avatar Apr 08 '24 19:04 Key5n

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

codesandbox-ci[bot] avatar Apr 08 '24 19:04 codesandbox-ci[bot]

Sure! Thank you for comment.

If you want to modify my PR, please do at your mercy.

Key5n avatar Jun 01 '24 10:06 Key5n

Sorry, I didn't notice that some tests were unpassed. I will fix it.

Key5n avatar Jun 07 '24 11:06 Key5n

@huozhi

A PR I submitted fails some tests, so I've changed almost of whole things and add tests. Sorry for begging you reviews again.

I changed the type KeyedMutator using overload function, making populateCache is necessary when MutationData (the type of the first argument of bound mutate) is different from the type of Data.

This achieves strict type attaching.

Key5n avatar Jun 17 '24 15:06 Key5n