Skip to content

immer as middleware and use devtools typescript #609

Closed
@anver

Description

@anver

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

image

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.

image

Code with squiggly typescript errors
image

when devtools removed it's ok
image

extra information from vscode screenshot

image

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptThis issue is about TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions