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

return void draft.count++ in TS #37

Closed
alexluong opened this issue Oct 30, 2019 · 4 comments
Closed

return void draft.count++ in TS #37

alexluong opened this issue Oct 30, 2019 · 4 comments
Labels
needs reproduction no reproduction has been provided. This issue is in-actionable and will be closed soon unless update

Comments

@alexluong
Copy link

alexluong commented Oct 30, 2019

When using useImmerReducer, I have return void draft.count++ but it doesn't seem to work. I'm quite new to both immer and TypeScript. If anyone knows what's going on, I'd really appreciate your help.

Thank you.

@danielkcz
Copy link
Contributor

danielkcz commented Oct 31, 2019

Huh, I've never seen return void draft.count++ before :D Surprisingly enough, it's syntactically correct.

Ah, it's actually in README. Looks like a typo to me, should be without that void if anything.

Either way, with Immer you don't need to be returning anything, just mutate values and done.

The return in reducer is just for convenience because of the switch so you can exit that specific case. Without return or break it would fall-through to the next case.

@mweststrate
Copy link
Collaborator

mweststrate commented Oct 31, 2019 via email

@mweststrate mweststrate added the needs reproduction no reproduction has been provided. This issue is in-actionable and will be closed soon unless update label Oct 31, 2019
@alexluong
Copy link
Author

alexluong commented Oct 31, 2019

@mweststrate you're right, my bad.

Like @FredyC suggested, not returning anything in the reducer function is fine. I didn't know that.

FWIW:

return void state.isOpen = true;

TS will underline = and says ';' expected.

If anyone wants to still use void, you can do:

return void (state.isOpen = true);

But again, this works perfectly fine too:

state.isOpen = true;
return;

@shadoath
Copy link

shadoath commented Nov 13, 2023

It would be good to update the readme to explain this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs reproduction no reproduction has been provided. This issue is in-actionable and will be closed soon unless update
Projects
None yet
Development

No branches or pull requests

4 participants