in the below, code action type (action as SucceededAction) is specified in the reducer and things are working but if I remove (as SucceededAction) I am getting an error 'payload does not exist on type 'SucceededAction | FailedAction'.'
Now if I replace the constants with --
type_SUCCEEDED:'type_SUCCEEDED',type_FAILED:'type_FAILED' and use them like type_FAILED and type_SUCCEEDED instead of type.FAILED, type.SUCCEEDED, it works without typecasting.
So, in the code below I think typeof is not working for my actions due to which I have to typecast in the reducers. Want to know the exact cause for the same. Please help if you know the reason and help me out here to find out the mistake in the code below.
Thanks.
//constants
export const type = {
SUCCEEDED: 'type_SUCCEEDED',
FAILED: 'type_FAILED'
};
export interface SucceededAction {
readonly type: typeof type.SUCCEEDED;
readonly payload: string;
}
export interface FailedAction {
readonly type: typeof type.FAILED;
}
//actions
export const Succeeded = (payload: string): SucceededAction => ({
type: type.SUCCEEDED,
payload,
});
export const Failed = (): FailedAction => ({
type: type.FAILED,
});
//reducer
export const myReducer = (
state: string = '',
action:
| SucceededAction
| FailedAction,
): string => {
switch (action.type) {
case type.SUCCEEDED:
return (action as SucceededAction) //type specified
.payload;
case type.FAILED:
return state;
default:
return state;
}
};