-
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
Cast permitted with missing properties on object literals #13788
Comments
What type assertion does, it tells the compiler to "shut up" and trust you. The operator behaves both as an upcast and as a downcast operator. The only check is that the one of the types is assignable to the other. In the example above, for |
Thank you - this is very helpful. At this risk of turning this into a question thread, it's not clear to me why asserting that { a:string, b?:string } is assignable to {} is useful in the case where the assignment is the other way around (from {} to { a:string, b?:string }). i.e. Why does it matter if the target type is assignable to the source type? Why check at all? |
Not sure i understand your question here. the compiler is trying to help verify that this is either a valid up cast or down cast, and not two completely unrelated types. beyond that, it is up to you to decide whether this was a valid conversion or not. |
TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
Code
Expected behavior:
Expected behavior is that test2 case is not permitted. NOTE: This was working correctly as of TypeScript 1.7.
Test3 is an example of how to perturb the "correct" behavior.
I have not been able to find any documentation about why test1 is allowed. One could make the case for filling out an empty object literal after it has been instantiated empty, but this seems like a dangerous thing to allow. Again it permits the case where we have an object of ITestOne that does not meet the interface definition.
Actual behavior:
test2 passed compiler checks, but produces an invalid object literal without property a.
See SO post for some further background:
http://stackoverflow.com/questions/41941125/intellisense-with-union-types
The text was updated successfully, but these errors were encountered: