Skip to content

How to type in Typescript the use of immer middleware with v4 createStore #1281

Answered by jonavila
jonavila asked this question in Q&A
Discussion options

You must be logged in to vote

@dai-shi I think I may have figure it out. My first instinct was to try to import the WithImmer type but noticed that wasn't available as an export. Here's an example on how I got it to work

import { createStore, useStore } from "zustand";
import { immer } from "zustand/middleware/immer";
import { createContext, FC, useContext } from "react";

interface MyStore {
  counter: number;
  inc: () => void;
}

const intialState = {
  counter: 5
};

const createMyStore = (state: typeof intialState = intialState) => {
  return createStore<MyStore, [["zustand/immer", never]]>(
    immer((set) => ({
      ...state,
      inc: () =>
        set((state) => {
          state.counter++;
        })
    }…

Replies: 3 comments 24 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
21 replies
@dai-shi
Comment options

@robxbob
Comment options

@dai-shi
Comment options

@robxbob
Comment options

@dai-shi
Comment options

Answer selected by jonavila
Comment options

You must be logged in to vote
3 replies
@gandhis1
Comment options

@acao
Comment options

@muzhaqi16
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
10 participants