You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expected behavior:
The code should not compile, because properties multipleOf and format don't exist in type Float.
Actual behavior:
The code compiles without errors. Typescript version 3.1.6 is the last version which behaves as expected and prints the error message.
Removing the Bool interface from the union, or changing the type parameter on the Bool interface to any other type causes expected behavior.
Not using inheritance and defining the value property in each interface separately does not change the behavior.
Looks like it's not specific to boolean but rather some kind of interaction with literal types (note: boolean is exactly true | false). Changing boolean to e.g. 1 | 2 or "foo" | "bar" also silences the excess property error, but number | string works correctly.
Excess property checking has special logic for discriminated unions to give better results when properties are part of the overall union, but not part of the variant you are using.
The issue here is that both type and value are flagged a discriminant properties---the latter because of the Bool variant. If EPC detects two discriminant properties in the source object that discriminate to different types then the logic I described above is skipped. The result is that you fail to get an error because multipleOf is not an excess property of the union Primitive.
I believe the correct fix would be to relax the condition in discriminateTypeByDiscriminableItems such that you are not required to discriminate to identical (identical here being pointer equality) types. A proposed fix would allow multiple matches if, after taking the intersection of matches across all discriminant properties, there was a single and therefore unambiguous match.
The property type would discriminate to Float, and the value property would discriminate to both Int and Float. As Float appears in all matches, and is the only type that appears in all matches, it would be the discriminated variant.
TypeScript Version:
3.6.0-dev.20190801
Search Terms:
discriminated boolean, tagged boolean
Code
Expected behavior:
The code should not compile, because properties
multipleOf
andformat
don't exist in typeFloat
.Actual behavior:
The code compiles without errors. Typescript version 3.1.6 is the last version which behaves as expected and prints the error message.
Removing the
Bool
interface from the union, or changing the type parameter on theBool
interface to any other type causes expected behavior.Not using inheritance and defining the
value
property in each interface separately does not change the behavior.Playground Link:
LINK
Related Issues: -
The text was updated successfully, but these errors were encountered: