简体   繁体   English

使用 Typescript 输入 Redux Store

[英]Typing Redux Store with Typescript

Here are the action creators types:以下是动作创建者类型:

    export type Calculation = {
      type: string;
    };
    
    export type AddAndSubtract = {
      type: string;
      value: number;
    };
    
    export type GetUserInput = {
      type: string;
      value: HTMLTextAreaElement;
    };

export type DispatchActions = Calculation | GetUserInput | AddAndSubtract;

And this is the store:这是商店:

export const rootReducer = (state = initialState, action: DispatchActions) => {
  switch (action.type) {
    case "INCREMENT":
      return { ...state, count: state.count + 1 };
    case "DECREMENT":
      return { ...state, count: state.count - 1 };
    case "ADD_5":
      return { ...state, count: state.count + action.value };
    case "SUBTRACT_5":
      return { ...state, count: state.count - action.value };
    case "RESET":
      return { ...state, count: action.value };
    case "GET_USER_INPUT":
      return { ...state, userInput: action.value };
    case "MULTIPLY":
      return {
        ...state,
        count: state.count * state.userInput,
      };
    default:
      return state;
  }
};

However typescript show error: Property 'value' does not exist on type 'DispatchActions'.但是打字稿显示错误:“DispatchActions”类型上不存在属性“value”。 Property 'value' does not exist on type 'Calculation'. “计算”类型上不存在属性“值”。 How may I solve this ?我该如何解决这个问题?

No need to use union type here.这里不需要使用联合类型。 You just need to create the single type DispatchAction:您只需要创建单一类型的 DispatchAction:

export type DispatchAction = {
      type: string;
      value: number | HTMLTextAreaElement;
}

export const rootReducer = (state = initialState, action: Partial<DispatchAction>) =>

OR或者

export type DispatchAction = {
      type: string;
      value?: number | HTMLTextAreaElement;
}

export const rootReducer = (state = initialState, action: DispatchAction) => ...

In Addition of above comment.除了上面的评论。 This will also work!这也将起作用!

Add value key in Calculation, you are using union type's.在计算中添加值键,您正在使用联合类型。

export type Calculation = {
      type: string;
      value: number
    };

Read https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html for more help and reason of error!阅读https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html以获得更多帮助和错误原因!

As others are indicating, the problem is coming from how you are using the union.正如其他人所指出的,问题在于您如何使用联合。 As you've written it, Typescript doesn't have any way of knowing which of the possible types in the union you are passing and so it has to assume it could be any of them.正如您所写的那样,Typescript 无法知道您正在传递联合中的哪些可能类型,因此它必须假设它可能是其中的任何一种。 For example, it would be perfectly valid with your typing to pass an action like this:例如,通过您的输入传递这样的操作是完全有效的:

const badAction:Calculation = {
  type: “SUBTRACT_5”
}

Which wouldn't work with your code at runtime—and that's exactly what TS is telling you.这在运行时不适用于您的代码——这正是 TS 告诉您的。 You could handle by having a weak type like the answers above are suggesting.您可以通过使用弱类型来处理,就像上面的答案所建议的那样。 Or you could use string literals to provide stronger typing with unions as explained in the Redux docs.或者您可以使用字符串文字来提供更强的联合类型,如Redux 文档中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM