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
What is the detail logic of type assertion? #23698
Comments
Docs are at: https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions Basically the compiler is checking that the two types are somehow related, that is that one is assignable to the other. In this case neither is the case. In one way the object literal is missing a a required property category, in the other way missing required property labs.a |
@mhegazy Can you explain a little more about why case 1&2 are no compilation errors? What's the difference between 1/2 and 3? |
Cuase they are assignable in one direction. |
@mhegazy Sorry, I still don't understand. I think the 3 object literals are not assignable to If it is hard to explain, can you give me a link to the related TypeScript source codes? |
There's a function in |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.8.1
Search Terms: type assertion
Code
Expected behavior:
The compiler always complains the missing of field
category
, or never. I know type assertion is not 100% safe, but am confused about its check logic. Can someone point out related codes for me? The ts code base is so complex.Actual behavior:
Only
makeMgeInfo3
throws an error about the missing of fieldcategory
.Playground Link: very long link
Related Issues:
I also asked on stackoverflow, but got no satisfying answers.
The text was updated successfully, but these errors were encountered: