首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带Redux设置的TypeScript CRA

带Redux设置的TypeScript CRA
EN

Stack Overflow用户
提问于 2020-03-10 18:28:28
回答 1查看 119关注 0票数 0

我正在尝试使用Create React App和Redux快速设置,我显然遗漏了一些东西……

Index.tsx

代码语言:javascript
复制
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux'
import { store } from './store/store'
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

const rootElement = document.getElementById('root')
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  rootElement
)
serviceWorker.unregister();

商店

代码语言:javascript
复制
import { createStore } from "redux";
import { rootReducer } from "../reducers/index";
const store = createStore(rootReducer);
//                           ^ error
export { store };

我得到的错误是

代码语言:javascript
复制
No overload matches this call.
  Overload 1 of 2, '(reducer: Reducer<{ articles: never[]; }, UserAction>, enhancer?: StoreEnhancer<unknown, unknown> | undefined): Store<{ articles: never[]; }, UserAction>', gave the following error.
    Argument of type '(state: { articles: never[]; } | undefined, action: UserAction) => { articles: any[]; }' is not assignable to parameter of type 'Reducer<{ articles: never[]; }, UserAction>'.
      Type '{ articles: any[]; }' is not assignable to type '{ articles: never[]; }'.
        Types of property 'articles' are incompatible.
          Type 'any[]' is not assignable to type 'never[]'.
            Type 'any' is not assignable to type 'never'.
  Overload 2 of 2, '(reducer: Reducer<{ articles: never[]; }, UserAction>, preloadedState?: { articles: never[]; } | undefined, enhancer?: StoreEnhancer<unknown, {}> | undefined): Store<{ articles: never[]; }, UserAction>', gave the following error.
    Argument of type '(state: { articles: never[]; } | undefined, action: UserAction) => { articles: any[]; }' is n

和减速机

代码语言:javascript
复制
const initialState = {
    articles: []
  };

  export interface UserAction {
    type: string;
    payload: any;
}

  const rootReducer = (state = initialState, action: UserAction) => {
    switch (action.type) {
        case 'ADD_ARTICLE': {
          return {
            ...state,
            articles: [action.payload, ...state.articles],
          };
        }
        default:
          return state;
      }
  }

  export { rootReducer };

应用程序

代码语言:javascript
复制
import React from "react";
import { rootReducer } from "./reducers/index";

function App() {
  const addArticle = () => {
    rootReducer({type: 'ADD_ARTICLE', payload: 'my new article'}) // this isnt right!
  };
  return <button onClick={addArticle}>Add Article</button>;
}

export default App;
EN

回答 1

Stack Overflow用户

发布于 2020-03-10 19:33:31

下面是一个具有显式类型的缩减程序的示例:

代码语言:javascript
复制
interface IArticles {
  articles: string[];
}

const initialState: IArticles = {
  articles: []
};

export interface UserAction {
  type: string;
  payload: string;
}

const rootReducer = (state = initialState, action: UserAction): IArticles => {
  switch (action.type) {
    case "ADD_ARTICLE": {
      return {
        articles: [action.payload, ...state.articles]
      };
    }
    default:
      return state;
  }
};

之前我已经经历过和你一样的问题,因为reducer中的类型是错误的。

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

https://stackoverflow.com/questions/60615728

复制
相关文章

相似问题

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