reactjs - 使用 TS : "Property ' __REDUX_DEVTOOLS_EXTENSION_COMPOSE_ _' does not exist on type ' Window'. 的 Redux DevTools 扩展出错?

标签 reactjs typescript redux react-redux redux-devtools-extension

我在 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。非常感谢您提供有关提前发生的事情的任何提示。

最佳答案

这是 this question 的特例. 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__

关于reactjs - 使用 TS : "Property ' __REDUX_DEVTOOLS_EXTENSION_COMPOSE_ _' does not exist on type ' Window'. 的 Redux DevTools 扩展出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800877/

相关文章:

node.js - 如何在 Electron 中使用 node_modules?

node.js - 部署到 Heroku 的 MERN 应用程序无法正常工作

react-native - 什么时候不应该在 React-Native 中使用 Redux?

javascript - Redux:在 reducer 中过滤数据数组的正确方法是什么?

reactjs - Redux 坚持重新水化先前的身份验证状态为时已晚

node.js - 更新到 Angular 8 CLI 后抛出 ".getColorDepth is not a function"

javascript - react-dates 组件不允许选择当前日期之前的日期

javascript - Angular 与 ngrx - 没有发生任何事情

reactjs - 找不到模块 : Can't resolve 'react-native'

javascript - 在 react 类中设置等待设置状态可以吗?