参考:官方文档 zustand slice
type Proj = {
id: number,
projName: string,
description: string
}
export const createProjSlice = (set:any, get:any) => ({
// 1.状态
projs: Proj[], // 报错
selectedProj: Proj, // 报错
// 2.操作状态的actions
increaseProj: () => set((state:any) => ({ projs: state.projs + ... })),
removeAllProjs: () => set({ projs: [] }),
})
请问应该如何定义slice呢?(需要结合TypeScript的类型)
尝试都报错:
projs: Proj[],
selectedProj: Proj,
===
store/index.ts为:
import { create } from 'zustand'
import { createTodoListSlice } from './todolist.store'
import { createBearSlice } from './bears.store'
export const useStore = create((set:any, get:any) => ({
...createBearSlice(set, get),
...createTodoListSlice(set, get)
}))
试试这个方法:
类型和值是分开的