-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unable to assert(implicit down cast) narrow types from intersection types with partial object types #7531
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
Comments
Do not really know how better to explain it than the error message does :)
|
We have the rule in type assertions that your assertion has to be plausibly correct. In other words, you can't write this code var x = 'hello';
var y = <number>x; // Nope because there's really no way that's true unless you've been telling some big lies about the type of something. So in this case, if you want this to be legal (as a suggested change, not a bug), we'd need to see some rule which a) makes this legal, b) has some compelling justification as for why it should be legal, and c) doesn't allow any random type to be asserted to any other random type. |
TypeScript implicitly destructs intersection types in declarations. var x: number & string;
var y: number = x; Why TypeScript does not have the same rule in partial object? |
There's no implicit destructuring there, just a simple rule of assignability. A simpler case to think about would be something like interface Farm1 {
x: Horse;
y: Animal;
}
interface Farm2 {
x: Animal;
y: Dog;
}
var f1: Farm1;
var f2: Farm2;
f1 = <Farm1>f2; This assertion isn't allowed because neither #5300 is closely related. |
I understood that. Thanks @RyanCavanaugh . |
TypeScript Version:
master
Code
Expected behavior:
Actual behavior:
The text was updated successfully, but these errors were encountered: