Skip to content

How to create a TypeScript wrapper for create + devtools + immer + combine? #2195

Answered by dai-shi
rodrigocfd asked this question in Q&A
Discussion options

You must be logged in to vote

Here you go: https://tsplay.dev/w1Qg2w

import { create, StateCreator, StoreMutatorIdentifier } from "zustand";
import { devtools } from "zustand/middleware";
import { immer } from "zustand/middleware/immer";

export function createOurStore<T extends object, U extends object>(
  name: string,
  state: T,
  actions: StateCreator<
    T,
    [["zustand/devtools", never], ["zustand/immer", never]],
    [["zustand/immer", never], ["zustand/devtools", never]],
    U
  >,
) {
  return create<T & U>()(
    devtools(
      immer((...a) => Object.assign({}, state, (actions as any)(...a))),
      { name },
    ),
  );
}

const useOrigStore =
  // ^?
  create<{
    name: string;
    setName: (name: s…

Replies: 1 comment 13 replies

Comment options

You must be logged in to vote
13 replies
@rodrigocfd
Comment options

@dai-shi
Comment options

@rodrigocfd
Comment options

@dai-shi
Comment options

Answer selected by rodrigocfd
@rodrigocfd
Comment options

@rodrigocfd
Comment options

@dai-shi
Comment options

@rodrigocfd
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
3 participants