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:
This should throw an error.
IMHO I think that there are 3 issues here.
First off I was surprised that numberTwo was allowed AT ALL. I think that the type should be either IOne OR ITwo, not a mix of the two.
If we do allowed mixed types (which I don't think we should) then surely if there are any properties for ITwo we should require all of them. This should throw an error unless we have specified both numberTwo and stringTwo
lastly it's not even type checking numberTwo. It is typed as a number but it's fine with a string.
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).
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: