-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Support Action Name in Typescript SetState #980
Comments
Vanilla store doesn't support action name. It's only available when you use |
As a developer I want to assign actions names to the actions I've defined in my store. Here is the definition: When you want to define an action name like below:
I receive a typescript error I think we need to change the type definition of |
Is this issue about We have types for zustand/src/middleware/devtools.ts Lines 53 to 55 in 89c3577
|
@dai-shi i'd say both import {GetState, State} from 'zustand'
type SetState<T extends State> = {
_(partial: T | Partial<T> | ((state: T) => T | Partial<T>), replace?: boolean | undefined): void, actionName?: string
}['_'];
export type StoreSlice<T extends object, E extends object = T> = (
set: SetState<E extends T ? E : E & T>,
get: GetState<E extends T ? E : E & T>
) => T;
export const useStore = create(devtools(createRootSlice)); |
That looks like a case with Can anyone open a new issue with a reproduction with typescript playground?
It's very intentional, so not an issue. Closing this. |
Hello i following this Typescript Example for creating separated splices. It gave me error like |
https://github.com/hasan-almujtaba/next-starter/blob/10ade3d52a2a0eba949de03215b7e7545dcc498d/store/example.ts#L7 You need to type function by yourself, if inline function doesn't work for you. const createExample = (set: (partial: (state: Store) => Partial<Store>, replace?: boolean, name?: string) => void, get: GetState<Store>) => ({
count: 0,
increment: () => {
set((prev) => ({ count: prev.count + 1 }))
},
}) Hope it works. |
It's a typescript issue, the type type SetState<T extends State> = {
_(partial: T | Partial<T> | ((state: T) => T | Partial<T>), replace?: boolean | undefined, actionName?: string): void;
}['_']; |
Copying my comment here from #1013 (comment) : Let's invite @devanshj to explain the background better. |
If one follows these intructions given in the docs for slices pattern one'd have no problems. In particular this instruction:
Following those instructions would lead to the following code, which I think is the one that is being desired... import create, { StateCreator } from 'zustand'
import { devtools } from "zustand/middleware"
interface BearSlice {
bears: number
addBear: () => void
}
const createBearSlice: StateCreator<BearSlice, [["zustand/devtools", never]], []> = (set) => ({
bears: 0,
addBear: () => set((state) => ({ bears: state.bears + 1 }), false, "addBear"),
})
const useStore = create<BearSlice>()(devtools((...a) => ({
...createBearSlice(...a)
}))) It's recommended to consult the docs before reporting an issue :) Zustand has complex types internally so that there are no complex types externally on users end. If one follows the docs one'd find one has to annotate and type so little. See #710 if you want to learn about the complex internal types.
I guess this sandbox is from the wiki? So I guess the issue here was that the old docs in the wiki were consulted instead of new ones. @dai-shi could you delete or redirect outdated wikis to new docs? |
Thanks for your suggestion, it's work as expected. |
Thanks for your instruction :) i'm following sandbox from this wiki |
Yeah I guessed that, no problems, it's our bad that we forgot to redirect the outdated wiki to new docs. |
Just one question left, how to stack other middleware like persist? Now i have following code // store/index.ts
import create from 'zustand'
import { devtools, persist } from 'zustand/middleware'
import { Store } from '../types/store'
import createExample from './example'
const useStore = create<Store>()(
devtools(
persist((...a) => ({
...createExample(...a), // error here
}))
)
)
export default useStore // store/example.ts
import { StateCreator } from 'zustand'
import { Store } from '../types/store'
const createExample: StateCreator<
Store,
[['zustand/devtools', never], ['zustand/persist', Store]],
[]
> = (set) => ({
count: 0,
increment: () => {
set((prev) => ({ count: prev.count + 1 }))
},
})
export default createExample // types/store.ts
export interface Example {
count: number
increment: () => void
}
export type Store = Example And this give me following error in
|
Yeah that's a bit of a limitation, instead of writing It won't make much of a difference, as you can see The second type parameter is for the partialised state, so unless you're using redefining options with |
Got it, thanks! |
Done. |
Hello, how to do indepent slice with persist and devtools middleware like this? here my example, this example give me error https://tsplay.dev/wXQAON |
That too is a (recently) known issue, see #1046 (comment) for a temporary fix. It probably will be fixed in next rc. |
I see, currently i try using interdependent slices and it's works as expected so for now i'm going to use this instead. |
I am trying this as well. How to set only one slice to persist when using the slices pattern though? |
@Mugilan-Codes you can use |
It came to our attention that the SetStateType action name is not defined in
zustand/src/vanilla.d.ts
.Even though it's written in the documentation
Suggestion to add the definition of the actionName to the type
The text was updated successfully, but these errors were encountered: