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
// A *self-contained* demonstration of the problem follows...consttypes={test: <'test'>'test',test2: 'test2'as'test2'}// type: {test: string, test2:string}leta=<'test'>'test'// type string
Yes, this is related to #10676. Previously, a string literal would have a literal type only in certain "literal contexts" (such as after a type assertion) and we would just use the exact type of an expression as the inferred type of an object literal property. Now, string literals always have literal types and instead we widen types when they're inferred for mutable locations (such as object literal properties). So, in your example, the type assertions have no effect (a) because the string literals are already of literal types and (b) because we widen the result of the entire expression after the type assertion.
For better backwards compatibility we could consider not widening an inferred type when the initializer expression is a type assertion. After all, what would be the point of the type assertion otherwise?
It would be great if you could consider not widening an inferred type when the initializer expression is a type assertion - and agree - that's kind of the point of the type assertion :)
Another common case that will stop working without it is:
@wallverb Agreed, it would be nice to have some form of syntax to indicate you don't want widening without having to explicitly state the type. But regardless, for backwards compatibility reasons we should allow type assertions to have that meaning.
TypeScript Version: 2.1.0-dev.20160912
Code
Probably related to: #10676
The text was updated successfully, but these errors were encountered: