Skip to content

Ban old-style type assertions under erasableSyntaxOnly - #61244

Merged
Jake Bailey (jakebailey) merged 6 commits into
microsoft:mainfrom
jakebailey:erasable-syntax-only-old-school-type-assertions
Feb 28, 2025
Merged

Jake Bailey (jakebailey) merged 6 commits into
microsoft:mainfrom
jakebailey:erasable-syntax-only-old-school-type-assertions

Conversation

@jakebailey

@jakebailey Jake Bailey (jakebailey) commented Feb 21, 2025 •

Copy link
Copy Markdown
Member

Code like ()=><any>{} is not erasable, since ()=> {} is a different tree, and there's nowhere to add parens to the body without shifting its contents backwards one character. If there were extra spaces after the {}, maybe it'd work, but that's atypical.

This PR bans this specific case, though, I personally feel like banning the old-style assertions would be a good idea too.

Updated the PR to just ban them. There are too may edge cases IMO.

@ajafff

Copy link
Copy Markdown
Contributor

you probably want to ban it in more places:

// return and yield ASI
function *foo() {
    yield <any>
        1;
    return <any>
        1;
}

// at the start of an ExpressionStatement if followed by an object literal; though I'm not sure why one would use it there
<unknown>{foo() {}}.foo();

// at the start of an ExpressionStatement if followed by function keyword
<unknown>function() {}();
<unknown>function() {};

// at the start of an ExpressionStatement if followed by an anonymous class expression
// note that this exact syntax currently emits invalid JS (no parenthesis added like for function above)
<unknown>class {}

there's probably more parsing ambiguity I cannot remember right now.

@jakebailey

Copy link
Copy Markdown
Member Author

Yeah, so that furthers my feeling that this syntax should be wholly banned in this mode. I tried to carve something out, but it sure doesn't seem like that's tractable if we want to ban something here.

@acutmore

Ashley Claymore (acutmore) commented Feb 26, 2025 •

Copy link
Copy Markdown
Contributor

When I was first creating ts-blank-space I tried to write the logic that only errors on the 'actual' bad cases but ended up coming to the conclusion that this may be more confusing to try and explain. This is why I ended up always erroring on the old style of type assertions and recommended code replaces them with as style assertions. The extra win of migrating to as is that code can more easily be moved to a .tsx file without getting sometimes cryptic errors.

@jakebailey Jake Bailey (jakebailey) changed the title Ban old-style type assertions as arrow function bodies under erasableSyntaxOnly Ban old-style type assertions under erasableSyntaxOnly Feb 26, 2025
@jakebailey

Copy link
Copy Markdown
Member Author

Updated the PR to just straight up ban the syntax.

@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Feb 28, 2025
@jakebailey
Jake Bailey (jakebailey) merged commit 1539234 into microsoft:main Feb 28, 2025
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Feb 28, 2025
@jakebailey

Copy link
Copy Markdown
Member Author

TypeScript Bot (@typescript-bot) cherry-pick this to release-5.8

@typescript-bot

TypeScript Bot (typescript-bot) commented Feb 28, 2025 •

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
cherry-pick this to release-5.8 ✅ Started ✅ Results

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey, Jake Bailey (@jakebailey)! I've created #61320 for you.

@jakebailey
Jake Bailey (jakebailey) deleted the erasable-syntax-only-old-school-type-assertions branch February 28, 2025 19:13
Comment thread src/compiler/checker.ts
Comment on lines +37316 to +37317
const start = node.type.pos - "<".length;
const end = skipTrivia(file.text, node.type.end) + ">".length;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned a different way of doing this in the pick PR, but this is probably not the right way to do this for non-syntactically correct code like

let x = <foo;

or

let y = <foo 123

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Jan 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants