Skip to content

Unify type assertion nodes #6099

Closed
Closed
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

Discussed in #6021

Originally posted by bradzacher May 31, 2020
There are two styles for type-assertions:

  • as assertions (x as type)
  • "angle bracket" assertions (<type>x)

Currently they produce two different, but almost identical ASTs:

const x = 1 as 1;
{
  "type": "TSAsExpression",
  "expression": { /* ... */ },
  "typeAnnotation": { /* ... */ }
}

and

const y = <1>1;
{
  "type": "TSTypeAssertion",
  "expression": { /* ... */ },
  "typeAnnotation": { /* ... */ }
}

I don't think that there's a reason why these should be separate AST nodes. I think that they're separate nodes because they have different code representations, but I don't know if that's something that matters from the POV of the AST. Similar to the discussion that's going on around the ESTree representation of optional chaining - there is a difference between the AST and the CST.

From the perspective of ESLint, having two nodes causes pains when writing lint rules, as authors have to remember to add both nodes to selectors or if checks. Often they don't, which means rules don't have proper support for TS syntax.

I propose that we unify the two nodes with the following AST:

interface TSTypeAssertion extends Node {
  type: AST_NODE_TYPES.TSTypeAssertion;
  typeAnnotation: TypeNode;
  expression: Expression;
  kind: 'as' | 'angle-bracket';
}
```</div>

Consider this issue a re-opening of #2142.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ASTPRs and Issues about the AST structurebreaking changeThis change will require a new major version to be releasedpackage: typescript-estreeIssues related to @typescript-eslint/typescript-estreewontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions