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
{{ message }}
Repository navigation
Unable to assert(implicit down cast) narrow types from intersection types with partial object types #7531
<{a: number,b: number}>{a: <number&string>0,b: 0};// ok<{a: number,b: number}>{a: <number>0};// ok<{a: number,b: number}>{a: <number&string>0};// error, should be able to assert
Expected behavior:
$ node built/local/tsc.js index.ts
Actual behavior:
$ node built/local/tsc.js index.ts
index.ts(3,1): error TS2352: Neither type '{ a: number & string; }' nor type '{ a: number; b: number; }' is assignable to the other.
Property 'b' is missing in type '{ a: number & string; }'.
We have the rule in type assertions that your assertion has to be plausibly correct.
In other words, you can't write this code
varx='hello';vary=<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.
This assertion isn't allowed because neither Farm1 nor Farm2 is assignable to the other. You can't have one property go "up" while the other goes "down" -- either all the properties are being asserted to a supertype, or all the properties are asserted to a subtype. There's no mixing and matching allowed.
Activity
mhegazy commentedon Mar 16, 2016
Do not really know how better to explain it than the error message does :)
RyanCavanaugh commentedon Mar 16, 2016
We have the rule in type assertions that your assertion has to be plausibly correct.
In other words, you can't write this code
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.
falsandtru commentedon Mar 16, 2016
TypeScript implicitly destructs intersection types in declarations.
Why TypeScript does not have the same rule in partial object?
RyanCavanaugh commentedon Mar 16, 2016
There's no implicit destructuring there, just a simple rule of assignability.
A simpler case to think about would be something like
This assertion isn't allowed because neither
Farm1norFarm2is assignable to the other. You can't have one property go "up" while the other goes "down" -- either all the properties are being asserted to a supertype, or all the properties are asserted to a subtype. There's no mixing and matching allowed.#5300 is closely related.
falsandtru commentedon Mar 16, 2016
I understood that. Thanks Ryan Cavanaugh (@RyanCavanaugh) .