gpt4 book ai didi

redux-localstorage 的 TypeScript 类型问题,persistState 不是一个函数

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:09 24 4
gpt4 key购买 nike

我想使用 redux-localstorage npm 模块将 redux stora 保存在本地存储中。问题出在 TypeScript 上,因为 redux-localstorage 没有类型定义。

所以我在 redux-localstorage.ts 中写了自定义类型定义:

declare module 'redux-localstorage' {
module ReduxLocalStorage {
export function persistState<A> (): any;
}
export = ReduxLocalStorage;
}

当我在 store.ts 中创建一个 redux store 时:

import { createStore, applyMiddleware, compose } from "redux";
import { syncHistoryWithStore, routerMiddleware } from "react-router-redux";
import { persistState } from "redux-localstorage";
import { browserHistory } from "react-router";

// Middleware
import logger from "./middleware/logger";
let ReduxThunk = require("redux-thunk").default;

// Reducers
import reducers from "./reducers/main";

export default function configureStore() {

const createStoreWithMiddleware: any = compose(
applyMiddleware(
ReduxThunk,
logger,
routerMiddleware(browserHistory)
),
persistState(),
window.devToolsExtension ? window.devToolsExtension() : f => f
)(createStore);

const store = createStoreWithMiddleware(reducers);
return {
store,
history: syncHistoryWithStore(browserHistory, store)
};
}

当我编译项目时,一切似乎都很好,但是当我尝试在 Chrome 中加载我的应用程序时,出现以下错误:

store.ts:21 Uncaught TypeError: redux_localstorage_1.persistState is not a function(…)configureStore @ store.ts:21(anonymous function) @ main.tsx:23__webpack_require__ @ bootstrap a9a3afb…:50(anonymous function) @ main.bundle.js?a9a3afb…:6__webpack_require__ @ bootstrap a9a3afb…:50webpackJsonpCallback @ bootstrap a9a3afb…:21(anonymous function) @ main.bundle.js?a9a3afb…:1

打字声明似乎至少以某种方式起作用,就像在我编写 redux-localstorage.ts 定义之前一样,我在项目构建时遇到以下错误:./src/store.ts 中的错误
(3,30):错误 TS2307:找不到模块“redux-localstorage”。

最佳答案

代替:

import { persistState } from "redux-localstorage";

尝试

import * as persistState from "redux-localstorage";

此外,您不需要创建类型定义文件...

关于redux-localstorage 的 TypeScript 类型问题,persistState 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41166630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com