【问题标题】:How to use zustand devtools with typescript?如何将 zustand devtools 与打字稿一起使用?
【发布时间】:2022-11-06 08:40:01
【问题描述】:

我正在尝试使用 zustand 的 devtools 功能。我也在使用打字稿。当我将商店传递给 devtools 时,我的整个商店代码会抛出错误:Argument of type 'StateCreator<Store, [], [["zustand/devtools", never]], Store>' is not assignable to parameter of type 'StateCreator<Store, [], [], Store>'.

这是我的商店代码:

const useUserStore = create<Store>(devtools((set) => ({
    token: "",
    isLogged: false,

    loginUser: (jwtToken: string) => {
        set((state) => ({
            ...state,
            token: jwtToken,
            isLogged: true,
        }));
    },

    logoutUser: () => {
        set((state) => ({
            ...state,
            token: "",
            isLogged: false,
        }));
    },
})));

这是商店类型:

export type Store = {
    token: string;
    isLogged: boolean;
    loginUser: (jwtToken: string) => void;
    logoutUser: () => void;
};

请指导我如何解决此错误。

【问题讨论】:

标签: typescript zustand


【解决方案1】:

尝试将create&lt;Store&gt;(devtools... 更改为create&lt;Store&gt;()(devtools...(注意额外的一对括号)

这是记录在on their Readme

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 2017-12-25
    • 2021-02-13
    • 2019-04-18
    • 2021-08-21
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多