Struggling with Redux code, I've encountered a peculiar behavior regarding type assignment that has left me puzzled.
In the following code snippet, it's clear that you cannot assign anyaction
to iaction
. Yet, surprisingly, assigning anyaction
to iaction2
works without any issues. Despite attempting to adhere to the "Assignment Compatibility" part of the specification, I can't quite grasp the reason behind this inconsistency.
What exactly is happening here? It seems apparent that there are instances of type AnyAction
that cannot be assigned to IAction2
.
export interface ILoadUserBegin {
type: "LOAD_USER_BEGIN";
}
export interface ILoadUserSuccess {
type: "LOAD_USER_SUCCESS";
response: number;
}
type IAction = ILoadUserSuccess;
type IAction2 = ILoadUserBegin | ILoadUserSuccess;
// these are redux types
interface Action {
type: any;
}
interface AnyAction extends Action {
[extraProps: string]: any;
}
// tests
let iaction: IAction = { type: "LOAD_USER_SUCCESS", response: 2 };
let iaction2: IAction2 = { type: "LOAD_USER_BEGIN" };
let anyaction: AnyAction = { type: "foo" };
// ERROR
// Type 'AnyAction' is not assignable to type 'ILoadUserSuccess'.
// Property 'response' is missing in type 'AnyAction'.
iaction = anyaction;
// But this is OK!
iaction2 = anyaction;