Next.js Discord

Discord Forum

slice pattern zustand with middlewares

Unanswered
Aditya Kirad posted this in #help-forum
Open in Discord
hey folks can anyone help me in implementing the slice patter in zustand with middleware here is my llittle code
export interface TodosProps {
  id: string;
  title: string;
  description: string;
  directory: string;
  date: string;
  completed: boolean;
  important: boolean;
}

interface TodosSlice {
  todos: Record<string, TodosProps>;
}

interface DirectoriesSlice {
  directories: string[];
  addDirectory: (directory: string) => void;
}

const createTodosSlice: StateCreator<
  TodosSlice & DirectoriesSlice,
  [
    ["zustand/immer", never],
    ["zustand/persist", Partial<TodosSlice>],
  ],
  [],
  TodosSlice
> = (set, get) => ({
  todos: initialTodos,
});

export const useTodos = create<TodosSlice & DirectoriesSlice>()(
  immer(
    persist((...a) => ({
        ...createTodosSlice(...a),
        ...createDirectoriesSlice(...a),
      }),
      {
        name: "instant-tasks",
      }
    )
  )
);

but I'm getting this error on
Argument of type 'Write<WithImmer<StoreApi<TodosSlice & DirectoriesSlice>>, StorePersist<TodosSlice & DirectoriesSlice, unknown>>' is not assignable to parameter of type 'Write<WithImmer<StoreApi<TodosSlice & DirectoriesSlice>>, StorePersist<TodosSlice & DirectoriesSlice, Partial<...>>>'.
  Type 'Write<WithImmer<StoreApi<TodosSlice & DirectoriesSlice>>, StorePersist<TodosSlice & DirectoriesSlice, unknown>>' is not assignable to type 'StorePersist<TodosSlice & DirectoriesSlice, Partial<TodosSlice>>'.
    The types of 'persist.setOptions' are incompatible between these types.
      Type '(options: Partial<PersistOptions<TodosSlice & DirectoriesSlice, unknown>>) => void' is not assignable to type '(options: Partial<PersistOptions<TodosSlice & DirectoriesSlice, Partial<TodosSlice>>>) => void'....

...a in ...createTodosSlice(...a)

0 Replies