-
Hi, I have a generic interface: interface StoreState<Item> {
items: Item[]
getItems: () => Item[]
//...
} How can I create a zustand store using it as the store type? I have tried |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
How about this? interface StoreState<Item> {
items: Item[];
}
const useStoreBase = create<StoreState<any>>((set) => ({
items: []
}));
export const useStore = <Item, Slice>(
selector: (state: StoreState<Item>) => Slice
) => useStoreBase(selector); |
Beta Was this translation helpful? Give feedback.
-
Hey @dai-shi , thanks this is already helpful but I can't get it to work. In this codesandbox I am using your example to specify the types of the store items. As soon as I pass a generic for items, the returned type is not correct anymore. Adjusted example: https://codesandbox.io/p/sandbox/modest-blackwell-m3mhxx?file=%2Fsrc%2FApp.tsx%3A20%2C1-21%2C1 It seems that I have a misunderstand of TS in this case but I can't figure out what's wrong. |
Beta Was this translation helpful? Give feedback.
How about this?
https://codesandbox.io/s/tender-bell-95sko3?file=/src/App.tsx