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

[use-global-hook] add setState overload for immer #57265

Closed
no-stack-dub-sack opened this issue Nov 19, 2021 Discussed in #57264 · 1 comment
Closed

[use-global-hook] add setState overload for immer #57265

no-stack-dub-sack opened this issue Nov 19, 2021 Discussed in #57264 · 1 comment

Comments

@no-stack-dub-sack
Copy link
Contributor

Discussed in #57264

Originally posted by no-stack-dub-sack November 18, 2021
When using immer, the setState function accepts a set state callback as the first argument. The current types do not support this:

export interface Store<S, A> {
    state: S;
    actions: A;
    setState(state: S, afterUpdateCallback?: () => void): void;
}

I propose adding an overload to setState so that this code will compile when using the Immer plugin.

The new overload could look like this:

export interface Store<S, A> {
    state: S;
    actions: A;
    setState(state: S, afterUpdateCallback?: () => void): void;
    setState(immerSetState: (state: S) => void): void;
}

Though I'm not sure the best way to do this because the overload will still be available when not using Immer and using it would cause a runtime error. Perhaps there's a better way to do this other than just adding an overload that's always present. Maybe something like this:

export type Store<S, A, WithImmer extends boolean = false> = WithImmer extends false ? {
    state: S;
    actions: A;
    setState(state: S, afterUpdateCallback?: () => void): void;
} : {
    state: S;
    actions: A;
    setState(immerSetState: (state: S) => void): void;
}

Thoughts?

@no-stack-dub-sack
Copy link
Contributor Author

closing after reading Moving DT issues to Discussions

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

1 participant