Here the accepted answer says that:
And the quirk of excess property checks is that for unions, it allows any property from any union constituent to be present in the assigned object literal.
But in this example:
type LoadingState = { isLoading: true };
type SuccessState = { isLoading: false; isSuccess: true };
type ErrorState = {
errorMessage: string;
};
type State = LoadingState | SuccessState | ErrorState ;
let x: State = {
isLoading: false,
isSuccess: true,
errorMessage: '',
};
Why am I getting following error?
Type '{ isLoading: false; isSuccess: true; errorMessage: string; }' is not assignable to type 'State'. Object literal may only specify known properties, and 'errorMessage' does not exist in type 'SuccessState'
errorMessage
comes from one of the unions as mentioned in that answer.
Extra: Another strange thing if I modify the State
type like this:
type State = SuccessState | ErrorState ;
error goes away.