I am relatively new to Typescript and am currently building a React app which includes Redux and am using Typescript.
Typescript is giving me the following error in my reducer;
ERROR in /Users/<REDACTED>/web/src/redux/reducers/steppedViews.ts
./src/redux/reducers/steppedViews.ts
[tsl] ERROR in /Users/<REDACTED>/web/src/redux/reducers/steppedViews.ts(13,53)
TS2339: Property 'activeView' does not exist on type '{ activeView: number; } | { totalViews: number; }'.
Property 'activeView' does not exist on type '{ totalViews: number; }'.
ERROR in /Users/<REDACTED>/web/src/redux/reducers/steppedViews.ts
./src/redux/reducers/steppedViews.ts
[tsl] ERROR in /Users/<REDACTED>/web/src/redux/reducers/steppedViews.ts(15,53)
TS2339: Property 'totalViews' does not exist on type '{ activeView: number; } | { totalViews: number; }'.
Property 'totalViews' does not exist on type '{ activeView: number; }'.
My action typ definition is as follows
export interface SetActiveView {
type: typeof types.steppedViews.SET_ACTIVE_VIEW;
payload: {
activeView: number;
};
}
export interface SetTotalViews {
type: typeof types.steppedViews.SET_TOTAL_VIEWS;
payload: { totalViews: number };
}
export type ReduxActionsSteppedViews = SetActiveView | SetTotalViews;
And my reducer is
import types from '../types';
import { ReduxActionsSteppedViews } from '../actions/steppedViews';
export interface ReduxStateSteppedView {
activeView: number;
totalViews: number;
}
const initialState: ReduxStateSteppedView = { activeView: 0, totalViews: 0 };
const reducer = (state = initialState, action: ReduxActionsSteppedViews): ReduxStateSteppedView => {
switch (action.type) {
case types.steppedViews.SET_ACTIVE_VIEW:
return { ...state, activeView: action.payload.activeView };
case types.steppedViews.SET_TOTAL_VIEWS:
return { ...state, totalViews: action.payload.totalViews };
default:
return state;
}
};
export default reducer;
I am confused as to why this is happening because from my understanding, the type defined is '{activeView: number;} | {totalViews: number;}' so it should be able to see either *.activeView or *.totalViews.
Any guidance on this error would be greatly appreciated. Thanks!