-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange behavior with union of complex types #23162
Comments
Related: #20863 There is nothing unsound about this interaction but it can be undesirable which is why there is excess property checking at initialisation, however I believe this does not work for unions without a discriminant (see related issue). |
I really can't see how you can say there is nothing unsound when I have a number property assigned to a string! |
The type
If you state
which can be satisfied with only Note: you can get unsound behaviour from this object: if ("numberTwo" in itemThree) {
itemThree.numberTwo // narrows to ITwo, unsafe!
} however this is from narrowing rules for union, not assignment compatibility. |
TypeScript Version: 2.8.1
Search Terms:
union complex types
Code
Expected behavior:
This should throw an error.
IMHO I think that there are 3 issues here.
numberTwo
was allowed AT ALL. I think that the type should be eitherIOne
ORITwo
, not a mix of the two.ITwo
we should require all of them. This should throw an error unless we have specified bothnumberTwo
andstringTwo
numberTwo
. It is typed as a number but it's fine with a string.Actual behavior:
The compiler is fine with this!
Playground Link:
Link
Work Around:
You can enforce proper type checking on the secondary mixed type like this:
The code above correctly fails to compile.
The text was updated successfully, but these errors were encountered: