代码之家  ›  专栏  ›  技术社区  ›  Hayk Safaryan

redux ts泛型类型“dispatch<s>”需要1个类型参数

  •  2
  • Hayk Safaryan  · 技术社区  · 6 年前

    尝试用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

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

    2 回复  |  直到 6 年前
        1
  •  1
  •   Matt McCutchen    6 年前

    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 node_modules/@types/react-redux

        2
  •  0
  •   EvgeniyRRU    6 年前

    Redux 3.7 Redux 4.0 here here