小编典典

有人使用redux开发工具在TS中遇到此错误吗?“类型'Window'上不存在属性'__REDUX_DEVTOOLS_EXTENSION_COMPOSE__'。”?

reactjs

我在index.tsx上收到此错误。

属性“ REDUX_DEVTOOLS_EXTENSION_COMPOSE ”在类型“窗口”上不存在。

这是我的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。非常感谢您提前进行的任何提示。


阅读 1334

收藏
2020-07-22

共1个答案

小编典典

这是这个问题)的特例。Redux不提供类型,__REDUX_DEVTOOLS_EXTENSION_COMPOSE__因为Redux
DevTools公开了该功能,而不是Redux本身。

可能是:

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

要么:

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

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

这已经由redux-devtools-extension包含TypeScript类型的包完成。如果已安装,则应使用其导入,而不是__REDUX_DEVTOOLS_EXTENSION_COMPOSE__手动访问。

2020-07-22