Given is a discriminated union type S1 | S2 | S3
:
type S1 = { d1: "foo"; }
type S2 = { d1: "bar"; isSuccess: true; }
type S3 = { d1: "baz"; isSuccess: false; errorMessage: string; }
type State = S1 | S2 | S3;
const testState: State = {
d1: "foo",
isSuccess: true, // no error, urgh..
errorMessage: "Error!" // no error, urgh..
// why no excess property check here?
}
I would expect TS to choose discriminant d1
, as it is the only property that exists in all three states and is a valid singleton type candidate. isSuccess
can't be chosen here, because it does not exist in S1
.
Assuming this behavior, why aren't excess property checks respected and no error emitted, as soon as I type isSuccess: true
, given d1
has value "foo"
?
Here is a test sample I made to understand the problem.