使用 TS 的 Redux DevTools 扩展出现错误:“类型‘Window’上不存在属性‘__REDUX_DEVTOOLS_EXTENSION_COMPOSE__’。”?

2024-01-26

我的 index.tsx 上出现此错误。

财产 'REDUX_DEVTOOLS_EXTENSION_COMPOSE' 在类型“Window”上不存在。

这是我的 index.tsx 代码:

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import registerServiceWorker from './registerServiceWorker';

import { Provider } from 'react-redux';

import { createStore, compose, applyMiddleware } from 'redux';
import rootReducer from './store/reducers';

import thunk from 'redux-thunk';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

 const store = createStore(rootReducer, composeEnhancers(
     applyMiddleware(thunk)
 ));

ReactDOM.render(  <Provider store={store}><App /></Provider>, document.getElementById('root'));

registerServiceWorker();

我已经安装了 @types/npm install --save-dev redux-devtools-extension 并且正在使用 create-react-app-typescript 。非常感谢您对提前发生的事情提供任何提示。


这是一个特殊情况这个问题 https://stackoverflow.com/questions/12709074/how-do-you-explicitly-set-a-new-property-on-window-in-typescript。 Redux 不提供类型__REDUX_DEVTOOLS_EXTENSION_COMPOSE__因为这个函数是 Redux DevTools 公开的,而不是 Redux 本身。

它是:

const composeEnhancers = window['__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'] as typeof compose || compose;

Or:

declare global {
    interface Window {
      __REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
    }
}

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

这已经由@redux-devtools/extension https://www.npmjs.com/package/@redux-devtools/extension包含 TypeScript 类型的包。如果已安装,则应使用其导入而不是访问__REDUX_DEVTOOLS_EXTENSION_COMPOSE__手动。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 TS 的 Redux DevTools 扩展出现错误:“类型‘Window’上不存在属性‘__REDUX_DEVTOOLS_EXTENSION_COMPOSE__’。”? 的相关文章

随机推荐