Closed
Description
This is a contrived example but the code works when using immer as middleware and I don't get any errors on vscode
import { Draft, produce } from 'immer';
import create, { GetState, SetState, State, StateCreator, } from 'zustand';
import { devtools } from 'zustand/middleware'
type Todo = { id: string; status: string; title: string }
type TState = { todos: Todo[], add(payload: Todo): void };
const sliceTodo = (set: SetState<TState>, get: GetState<TState>): TState => ({
todos: [],
add(payload) { set(state => { state.todos.push(payload) }) }
});
const immer = <T extends State>
(config: StateCreator<T>): StateCreator<T> =>
(set, get, api) =>
config((partial, replace) => {
const nextState = typeof partial === 'function'
? produce(partial as (state: Draft<T>) => T)
: partial as T
return set(nextState, replace)
},
get,
api
)
const useStore = create<TState>(immer((set, get) => ({ ...sliceTodo(set, get) })))
export default useStore
but when I use devtools middleware then I get errors
const useStore = create<TState>(
devtools(
immer((set, get) =>
({ ...sliceTodo(set, get) })
))
)
One thing I love about zustand is the redux devtools middleware, please someone help fix this issue, thx.
Code with squiggly typescript errors
extra information from vscode screenshot