简体   繁体   English

类型中缺少类型 { 错误}<TYPE_A> , 但在类型上是必需的<TYPE_B> - Redux 特定问题

[英]Type { error } is missing in Type <TYPE_A>, but is required in type <TYPE_B> - Redux Specific Problem

I have a small problem, typing my Redux Reducer.我有一个小问题,输入我的 Redux Reducer。 I know this question, has been generally answered, but I find the solutions lacking, and I would call them more workarounds than anything else.我知道这个问题已得到普遍回答,但我发现缺少解决方案,我认为它们是比其他任何方法都多的解决方法。

The Problem: I have a Redux Reducer.问题:我有一个 Redux Reducer。 Inside it, 3 functions managing state for Loading , Success and Failure , supplying the necessary data or errors in state along the way.在其中,有 3 个函数管理LoadingSuccessFailure状态,并在此errors中提供必要的data或状态errors And now TS comes into the game.现在 TS 加入了游戏。 Basically I get this error:基本上我得到这个错误:

TS2418: Type of computed property's value is
'(state: ImmutableObjectMixin<IState>, { error }: ActionFailure<Type.LIST_ITEMS_FAILURE, HttpError>) => ImmutableObjectMixin<...>',
which is not assignable to type '(state: ImmutableObjectMixin<IState>, action: ActionWithPayload<Type.LIST_ITEMS_SUCCESS, IItems[]>, { error }: ActionFailure<Type.LIST_ITEMS_FAILURE, HttpError>) => ImmutableObjectMixin<...>'.

Types of parameters '__1' and 'action' are incompatible.
Property 'error' is missing in type 'ActionWithPayload<Type.ITEMS_SUCCESS, IItems[]>' but required in type 'ActionFailure<Type.LIST_ITEMS_FAILURE, HttpError>'.

The error appears in the below block:错误出现在以下块中:

const ACTION_HANDLERS: {
  [key: string]: (
    state: ImmutableObjectMixin<IState>,
    action: ActionWithPayload<Type.LIST_ITEMS_SUCCESS, IItems[]>,
    { error }: ActionFailure<Type.LIST_ITEMS_FAILURE, HttpError>
  ) => ImmutableObjectMixin<IState>;
} = {
  [Type.LIST_ITEMS_ATTEMPT]: onListFetching,
  [Type.LIST_ITEMS_SUCCESS]: onListFetchingSuccess,
  [Type.LIST_ITEMS_FAILURE]: onListFetchingFailure // This line throws the TS Error
};

Now, I read about providing the error , to all possible properties, or making them all optional, in relevant answers, but they are not really solving my problem.现在,我在相关答案中阅读了有关向所有可能的属性提供error或将它们全部设为可选的内容,但它们并没有真正解决我的问题。

Here are the functions:以下是功能:

const onListFetching = (state: ImmutableObjectMixin<IState>): ImmutableObjectMixin<IState> =>
  state.merge({
    listFetching: true,
    list: [],
    errorListing: null
  });

const onListFetchingSuccess = (
  state: ImmutableObjectMixin<IState>,
  action: ActionWithPayload<Type.LIST_ITEMS_SUCCESS, IItems[]>
): ImmutableObjectMixin<IState> => {
  const { payload = [] } = action;
  return state.merge({
    listFetching: false,
    list: payload,
    errorListing: null
  });
};

const onListFetchingFailure = (
  state: ImmutableObjectMixin<IState>,
  { error }: ActionFailure<Type.LIST_ITEMS_FAILURE, HttpError>
): ImmutableObjectMixin<IState> =>
  state.merge({
    listFetching: false,
    errorListing: error,
    list: []
  });

Any help typing this would be appreciated.任何键入此内容的帮助将不胜感激。 I think the problem, might like on the key property.我觉得这个问题,可能就在关键属性上。 Since we are restructuring error but I could be wrong.由于我们正在重组error但我可能是错误的。 Thanks you..谢谢..

Instead of typing it on my own, I used, the Handlers Generic interface, from redux-sauce .我没有自己输入,而是使用了redux-sauce的 Handlers Generic 接口。

Here is the interface itself:这是界面本身:

  export interface Handlers<S> {
    [type: string]: (state: S, action: Action) => S;
  }

It basically does the exact same thing as my typing but it overlaps the TS Error.它基本上与我的打字完全相同,但它与 TS 错误重叠。 Not sure why, though.不过不知道为什么。 That would be useful to know, if you have the answer.如果您有答案,那么知道这将很有用。

Here is the new Code:这是新代码:

const ACTION_HANDLERS:Handlers<ImmutableObjectMixin<IState>> = {
  [Type.LIST_ITEMS_ATTEMPT]: onListFetching,
  [Type.LIST_ITEMS_SUCCESS]: onListFetchingSuccess,
  [Type.LIST_ITEMS_FAILURE]: onListFetchingFailure
};

Everything works like a char,.一切都像一个字符,。 But if it can be improved even more, please let me know..但如果它可以进一步改进,请告诉我..

暂无
暂无

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

相关问题 类型中缺少属性,但类型中需要属性 - Property is missing in type but required in type 类型“{}”中缺少属性“submitAction”,但在类型中是必需的 - Property 'submitAction' is missing in type '{}' but required in type Typescript 错误:类型中缺少属性“children”,但类型“CommonProps”中是必需的 - Typescript error: Property 'children' is missing in type but required in type 'CommonProps' 类型 A 缺少类型 B 的以下属性:a、b - Type A is missing the following properties from type B: a, b 属性 'id' 在类型 A 中是可选的,但在 if 语句之后在类型 B 中是必需的 - Property 'id' is optional in type A but required in type B after if statement 错误:类型“{}”缺少类型中的以下属性 - Error: Type '{}' is missing the following properties from type 类型中缺少 TypeScript 属性“导航”,但类型“道具”React Native 中需要 - TypeScript Property 'navigation' is missing in type but required in type 'Props' React Native 类型“ any []”中缺少属性“ 0”,但是类型“ [{{id:string; gp:布尔值; }] - Property '0' is missing in type 'any[]' but required in type '[{ id: string; gp: boolean; }] “ReactElement”类型中缺少属性“...”<any, any> ' 但在类型 '{...}' 中是必需的</any,> - Property '...' is missing in type 'ReactElement<any, any>' but required in type '{...}' '{ rating: any; 类型中缺少属性 'onClick' }' 但在“RatingType”类型中是必需的 - Property 'onClick' is missing in type '{ rating: any; }' but required in type 'RatingType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM