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

Type errors when extraReducers _after_ reducers in createSlice #2862

Closed
GYatesIII opened this issue Nov 3, 2022 · 6 comments
Closed

Type errors when extraReducers _after_ reducers in createSlice #2862

GYatesIII opened this issue Nov 3, 2022 · 6 comments

Comments

@GYatesIII
Copy link

Description

Under specific conditions, detailed below, some slices are typed with a WritableDraft wrapper. All objects within that slice are also wrapped. This causes type errors in consumers that are expecting the unwrapped types of slices & slice values.

image

Repro

Minimal repro repo:
https://github.com/GYatesIII/rtk-extra-reducers-issues

From my testing, I identified the specific conditions for repro:

  1. Use latest @reduxjs/toolkit (1.8.6) - I also observed the same behavior in 1.6.2 & 1.7.2
  2. TypeScript (I repro'd in both 4.7.4 & 4.8.4)
  3. Have TSConfig compilerOptions.strictFunctionTypes set to false
  4. Have a slice that has a reducer that accesses state param
  5. Have a slice that has an extra reducer function that access builder param
  6. Have the slice options define the extra reducer function key AFTER the reducer object
@GYatesIII
Copy link
Author

@markerikson
Copy link
Collaborator

Thanks for the repro and the investigation!

My first thought is that we have to assume that users have all "strict"-type options turned on, so there may not be something we can actually do on our end.

@GYatesIII
Copy link
Author

Honestly, I think that's a fair stance to take, strict is best-practices. But in recognition of that stance, it should be noted obviously in the docs. Probably on this page: https://redux-toolkit.js.org/usage/usage-with-typescript

@brendanmc6
Copy link

Found a solution: https://stackoverflow.com/a/74468642/4458849

@okcoker
Copy link

okcoker commented Jan 27, 2023

My first thought is that we have to assume that users have all "strict"-type options turned on, so there may not be something we can actually do on our end.

In my case I was testing out RTK using a next.js example so the strictFunctionTypes option was completely omitted (although I've submitted a PR in light of this issue)

It should be noted obviously in the docs. Probably on this page: https://redux-toolkit.js.org/usage/usage-with-typescript

Agreed. I went through the docs as I wanted to try and find out about some odd behavior happening with typings of WritableDraft<T> vs T when using useAppSelector. I eventually found that for a given state, T, it seemed to only be WritableDraft<T> when I was populating the reducers option within createSlice, but would be T otherwise. I still wasn't getting correct typing on nullable state fields though but this was all highlighted as expected after adding strict: true to the tsconfig.

@phryneas
Copy link
Member

@okcoker yeah, unfortunately next should generally come with a warning label. The TS team intends strict to be the default for every new project, but for some reason, next has been avoiding this for years.

ijjk pushed a commit to vercel/next.js that referenced this issue Jan 28, 2023
I was updating one of my old next.js v3 apps to v13, and decided to play
around with RTK. Using the with-redux example sort of sets users up for
some weird typing issues as mentioned here:
reduxjs/redux-toolkit#2862

I found that github issue _after_ reading through all the official RTK x
TS docs, and it seems it could have all been avoided with stricter
typing. I'm hoping RTK can add this bit to their docs soon.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants