首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Redux泛型类型'Dispatch<S>‘需要一个类型参数

Redux泛型类型'Dispatch<S>‘需要一个类型参数
EN

Stack Overflow用户
提问于 2018-08-02 14:00:37
回答 2查看 5.7K关注 0票数 4

尝试用类型记录和redux设置一个项目。我收到了这个错误

代码语言:javascript
复制
Generic type 'Dispatch<S>' requires 1 type argument(s).

这是我的store.ts

代码语言:javascript
复制
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

这是根减速器

代码语言:javascript
复制
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'
})

下面是一些愚蠢的行为

代码语言:javascript
复制
import { action } from 'typesafe-actions'

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

最后是index.ts

代码语言:javascript
复制
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似乎也有类似的问题

但我对打字很陌生,所以可能缺少一些基本的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-02 17:36:31

在我看来,你确实面临着同样的问题。当我们等待并查看7mllm7的拉请求是否合并时,您可以使用他修改后的react类型。我建议采取以下方法:

  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-redux的版本指定为./react-redux.fixed
  5. 运行npm install。npm将建立一个从node_modules/@types/react-reduxreact-redux.fixed的符号链接。

与仅在node_modules/@types/react-redux中编辑文件相比,npm知道您使用的是修改后的包版本,不会覆盖它。(这个过程应该广为人知;如果我有时间,我会找到一个更好的地方来记录它。)

票数 2
EN

Stack Overflow用户

发布于 2018-10-02 19:52:57

我通过降级到Redux 3.7来解决这个问题。它有正确的类型(仍然没有Redux 4.0的类型)。在他们讨论这个问题的时候,有一些吉特布问题(这里这里)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51655546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档