I have a reducer that has an action creator that can be an array of two different types of object, each has their own interface. However, I am getting this error
Type '(A | B)[]' is not assignable to type 'B[]'.
Type 'A | B' is not assignable to type 'B'.
Property 'productionId' is missing in type 'A' but required in type 'B'
I suspect this error is due to both interface having similar values, except B has one extra value than A?
Here is the typescript playground
Here is the full reproducible code
interface A {
id: number;
name: string;
}
interface B {
id: number;
productionId: number;
name: string;
}
interface IAction<Data> {
type: string;
data: Data;
}
interface ISelectionOptionsReducerState {
a: A[];
b: B[];
}
let initialState: ISelectionOptionsReducerState = {
a: [],
b: []
};
type TAction = IAction<Array<A | B>>;
type TAction = IAction<A[] | B[]>; <= this didn't work either
type TReducer = (
state: ISelectionOptionsReducerState,
action: TAction
) => ISelectionOptionsReducerState;
const selectionOptionsReducer: TReducer = (
state: ISelectionOptionsReducerState = initialState,
action: TAction
): ISelectionOptionsReducerState => {
Object.freeze(state);
let newState: ISelectionOptionsReducerState = state;
switch (action.type) {
case '1':
newState.a = action.data;
break;
case '2':
newState.b = action.data; <= error happen here
break;
default:
return state;
}
return newState;
};