[英]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 个函数管理
Loading
、 Success
和Failure
状态,并在此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.