Allowed AsExpression-like type assertions on object literal shorthands #21855
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a
type?: TypeNode
property to theShorthandPropertyAssignment
interface, and adds it to the allowed tokens for shorthand property assignments in parsing. In checking, if a member type is declared on a shorthand assignment, it is used instead of the name.I was uncertain how best to describe this AST behavior. Two other options I decided against were:
AsExpression
instead of aShorthandPropertyAssignment
: would break the existing assumption that allproperties
on anObjectLiteral
contain a.name
.AsExpression
: thename
of the shorthand node would point to the same area as the expression's.type
.Open issues:
Intentionally leaves out the case of
<type>name
type assertions. I would love to get to those in a separate PR, but it looks like those would need different changes. Same PR for coherency or different PR for smaller changes?An object shorthand's
type
being before its name would imply it's a type assertion, while being after the name implies it's anas
assertion. That's the smallest API change but also a little hacky feeling. Would love to know of a preferred way... Perhaps having either anasType
andassertionType
but not both?IsasType.parent = node;
the correct way to set up the child node's.parent
? Without that line, in local tests it would fail on a union type (e.g.age as number | string
) with:~~...which maps tonode.parent.kind
.There's a test failing for it, but I couldn't figure out where the right place is.Found it: added it to
forEachChild
.Fixes #13035.