No excess property check for value with valid discriminated union type #35890
Labels
Bug
A bug in TypeScript
Design Limitation
Constraints of the existing architecture prevent this from being fixed
Milestone
TypeScript Version: 3.7.2, Nightly
Search Terms: discriminated union, discriminant, excess property check, literal type, singleton type
Code
Expected behavior:
After having set
d1: "foo"
, TS should recognize that object literaltestState
belongs toS1
and emit an error when trying to add excess propertiesisSuccess
and/orerrorMessage
. There should be no other discriminant candidate thand1
, because onlyd1
exists in all three states.Actual behavior:
There is no compile error.
Playground Link:
here
Related Issues:
Stackoverflow question
#32657 (maybe)
It kinda seems,
isSuccess: boolean
is interpreted as a second discriminant candidate. You can also substituteisSuccess: true
/isSuccess: false
withisSuccess: 1
/isSuccess: 0
and neither get an error. An error is only raised, whenisSuccess
is changed to a non-literal type likestring
. ButisSuccess
shouldn't be treated as discriminant in the first place!The text was updated successfully, but these errors were encountered: