Typescript issues with slices pattern #2491
Replies: 4 comments 4 replies
-
https://tsplay.dev/wX0aVw |
Beta Was this translation helpful? Give feedback.
-
Typescript in general can detect this error, and in this context it's extremely useful since if you were to change the name of a key in the store, a bug would be created since the old key would be silently accepted. I'd think there's an
It's not clear to me what version of zustand the playground is running, but I did post an example repo that reproduces the issue. This repo was bootstrapped simply with
It's certainly possible there's some configuration issue at play (which is why I posted a reproduction repo), but from what I can tell both of these items are real issues. |
Beta Was this translation helpful? Give feedback.
-
I've made some progress here. The second issue is solved by installing As for the first issue, it appears if you are using the
So the I guess the question is whether you are still allowed to use the vanilla zustand If that's the expected behavior, then I think the immer middleware's
This catches all of the errors in my Playground link, by making sure that if you do return something that it is the right type, but I'm not 100% sure it's a fully robust solution or the optimal fix. |
Beta Was this translation helpful? Give feedback.
-
yeah,
it doesn't. there's no
Good point. I think there's a historical reason. Probably, we should drop supporting returning partial state object. (This is breaking, so let's do it in v5.) |
Beta Was this translation helpful? Give feedback.
-
It looks to be common that Typescript issues are submitted without following the TS docs, but hopefully this is not is the case here since I am trying to follow those docs. There are 2 issues that I'm seeing when following these TS docs for the slice pattern:
state
type is not inferred properlyHere is a reproduction repo with
zustand@4.5.2
.index.ts
is almost exactly the same as the slices pattern TS doc, with a couple of tweaks:set
Both of these "errors" compile successfully with no warning (and
strict: true
in tsconfig.json). Are these behaviors expected? Is something wrong with the code? I tried to keep the changes minimal from the official docs.Searching this forum for similar posts I came across this code sample. This exact code, with some bad state setters added, does not error for me with current zustand.
Is it possible there have been some changes to the slices pattern for TS that have not been reflected in the docs? Maybe I'm missing something obvious but I couldn't figure it out.
Thanks for any insight!
Beta Was this translation helpful? Give feedback.
All reactions