Redux TS 通用类型“Dispatch<S>”需要 1 个类型参数

IT技术 reactjs typescript redux
2021-04-06 07:45:04

尝试使用 typescript 和 redux 设置项目。我收到此错误

Generic type 'Dispatch<S>' requires 1 type argument(s).

这是我的 store.ts

import { connectRouter, routerMiddleware } from 'connected-react-router'
import { applyMiddleware, compose, createStore } from 'redux'
import { createLogger } from 'redux-logger'
import ReduxThunk from 'redux-thunk'

import { createBrowserHistory } from 'history'

import reducers from './reducers'

import { composeWithDevTools } from 'redux-devtools-extension'

export const history = createBrowserHistory()

const composeEnhancers = composeWithDevTools({
  // Specify name here, actionsBlacklist, actionsCreators and other options if needed
})

const logger = createLogger()

const middleware = [ReduxThunk, logger]

const Store = createStore(connectRouter(history)(reducers), {}, composeEnhancers(applyMiddleware(...middleware, routerMiddleware(history))))

export default Store

这是根减速器

import { combineReducers } from 'redux'
import { ActionType } from 'typesafe-actions'

import * as actions from '../actions'

export interface IState {
   test: string
}

export type Actions = ActionType<typeof actions>

export default combineReducers<IState, Actions>({
  test: () => 'hey'
})

这里有一些虚拟动作

import { action } from 'typesafe-actions'

export const toggle = (id: string) => action('TOGGLE', id)
// (id: string) => { type: 'todos/TOGGLE'; payload: string; }

最后这里是 index.ts

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

import store, { history } from './store'

import { Provider } from 'react-redux'
import { Route, Switch } from 'react-router' // react-router v4
import { ConnectedRouter } from 'connected-react-router'

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}> { /* place ConnectedRouter under Provider */}
      <div> { /* your usual react-router v4 routing */}
        <Switch>
          <Route exact path="/" render={() => (<div>Match</div>)} />
          <Route render={() => (<div>Miss</div>)} />
        </Switch>
      </div>
    </ConnectedRouter>
  </Provider>,
  document.getElementById('root') as HTMLElement
)
registerServiceWorker()

这里似乎是一个没有解决方案的类似问题 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/9611

但我是typescript的新手,所以可能会遗漏一些基本的东西

2个回答

在我看来,您确实面临与您链接的相同问题。在我们等待 7mllm7 的 pull request 是否合并的同时,您可以使用他的 react-redux 类型的修改版本。我推荐以下方法:

  1. git clone --depth=1 https://github.com/7mllm7/DefinitelyTyped
  2. types/react-redux文件夹复制到您的项目中(例如,假设您将其复制到名为 的文件夹react-redux.fixed)。
  3. 编辑react-redux.fixed/package.json以替换"private": "true""name": "@types/react-redux".
  4. 在您的 中package.json,指定@types/react-reduxas的版本./react-redux.fixed
  5. 运行npm installnpm 将创建一个从node_modules/@types/react-reduxto的符号链接react-redux.fixed

与仅在node_modules/@types/react-redux. (这个过程值得广为人知;如果我有时间,我会找到一个更好的地方来记录它。)

尝试删除react-redux-tests.tsx文件或排除react-redux.fixed文件夹tsconfig.jsonindex.d.ts您的导入仍应将其拉入)。这是直接从 DefineTyped 复制类型目录而不是使用构建的 npm 包的一个小故障。
2021-05-31 07:45:04
是的,v5文件夹也需要排除或删除;它有一个不兼容的以前版本的 react-redux 类型。希望能修复所有错误。
2021-06-01 07:45:04
我按照你说的做了,这就是我现在得到的 /home/hayk/projects/elboil/app/react-redux.fixed/react-redux-tests.tsx (1,21): 'ReactElement' is declared but its value is never read.
2021-06-07 07:45:04
是的,修复了这个错误,但仍然引发另一个错误 /home/hayk/projects/elboil/app/react-redux.fixed/v5/index.d.ts (29,35): Type 'S' does not satisfy the constraint 'Action<any>'.
2021-06-07 07:45:04
做了上面建议的一切;问题依然存在;投降和使用dispatch: React.Dispatch<any>
2021-06-08 07:45:04

我通过降级到Redux 3.7. 它有正确的打字(仍然没有打字Redux 4.0)。他们讨论了一些 Github 问题(这里这里)。