[Literals][Indexes][Type assertion] Is that a bug? #14093
Description
Activity
as Tand<T>are identical in effect. They are just two syntacytic forms for the same operation, type assertion.Type assertion checks for a valid conversion from one type of the other, this can be in both directions, i.e. up/down cast. In this example, neither types is assignable to the other, so
{prop: string}is not assignable toT, sincepropis not"literal", nor isTassignable to{prop: string}sinceTdoes not have the required property calledprop, all it has is a constraint, that properties will be of type"literal".The real issue is that a string literal type in a mutable location (i.e.
let, property declaration or array literal member) are not reflected in the type unless stated at declaration time. SoDOOMEDneeds to have a typelet DOOMED: Tor the property needs to have a type assertion:{ prop:'literal' as 'literal' }- addedDesign LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
on Feb 15, 2017 RyanCavanaugh commented
on Feb 15, 2017 MemberMore actionsAlso,
DOOMEDhas the type{ prop: string }; this is a type that we don't know isn't aliased by some value that has properties which are not of typestring.or the property needs to have a type assertion: { prop:'literal' as 'literal' }
Well I already know that but what if I want to assert like a boss (
asor<>) keeping my code simple and clean from extralet DOOMEDdeclarations or assertion of thousands of existing'literal's in objects? 😎
There has to be the way any way.Type assertion checks for a valid conversion from one type of the other,
Why? Aren't they expected to convert a variable type into provided one?
Sounds like a mess.Why? Aren't they expected to convert a variable type into provided one?
Sounds like a mess.if all what you want is to make the assignment work, assert to
any.if all what you want is to make the assignment work, assert to any.
No way!
No way!
?
you do not want the compiler to check that one is assignable to the other, nor do you want to shut it up usingany.Explicit variable type declaration works well but what this issue is up to is that type assertion actually doesn't works as expected. That's why it can't be shut up just by
any-ing eveyything. If I useany, then it senseless to use types.RyanCavanaugh commented
on Feb 15, 2017 MemberMore actionsProbably best to read https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html
Well thanks. let's read the first line:
TypeScript allows you to override its inferred and analyzed view of types any way you want to.
RyanCavanaugh commented
on Feb 15, 2017 MemberMore actionsGood start - keep going 😉
- locked and limited conversation to collaborators
on Jun 19, 2018
Literal types related topics & conversations
Related to this issue PR that regulates literal type inference and its behavior in self-type changing can be found here: #10676
TypeScript Version: 2.1.6
Code
Expected behavior:
Type assertion is expected to work in both cases
<>andasbut doesn'tActual behavior:
Fails
Error: