Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse error on type assertion with contextual keyword in parentheses (override as Type) #44466

Closed
zxbodya opened this issue Jun 6, 2021 · 5 comments · Fixed by #49029
Closed
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Help Wanted You can do this
Milestone

Comments

@zxbodya
Copy link

zxbodya commented Jun 6, 2021

Bug Report

🔎 Search Terms

typecast, parentheses, override

🕗 Version & Regression Information

Started to see the issue after updating from TypeScript 4.2 to 4.3, initially noticing this when running prettier after updating it prettier/prettier#11018

It also is happening in current 'Nightly' version in the playground.

⏯ Playground Link

Playground link with relevant code

💻 Code

const override = {};
const b = (override as any);

🙁 Actual behavior

Code results in a syntax error.

However it works fine when renaming variable override to something else.

🙂 Expected behavior

Code above should work

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 7, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 7, 2021
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Jun 7, 2021
@ShuiRuTian
Copy link
Contributor

The reason is override become a new keyword. This is something like you could not name the variable to public, function, const....
Because when scanning, tokenlizer would scan this word as XXXKeyword rather than Identifier.
So, this is a breaking change, rather than a bug?

@DanielRosenwasser
Copy link
Member

@ShuiRuTian override is a contextual keyword, and TypeScript needs to support valid uses of it in JS.

From my comment at #44996 (comment)

Ugh - the same is true of (at least) readonly, declare, and abstract.

@DanielRosenwasser DanielRosenwasser changed the title Bug parsing typecast expression in parentheses Parse error on type assertion with contextual keyword in parentheses (override as Type) Jul 13, 2021
@ShuiRuTian
Copy link
Contributor

@DanielRosenwasser interesting, if so , I want to have a try to fix this issue.

@DanielRosenwasser
Copy link
Member

Go for it, though I don't know what precisely to do here.

Maybe when parsing a parameter for anything other than a constructor declaration, a keyword that isn't reserved in JS (such as override, readonly, declare, and abstract) should be re-scanned as a plain Identifier.

@DavidSouther
Copy link
Contributor

In tryParseParenthesizedArrowFunctionExpression, the lookahead treats this as OpenParen Modifier Identifier, when it should not expect as to be an identifier. I'm not sure if this is the correct or most robust fix, but it feels heuristically right for this and attached bugs, and I can't think of other parenthesized expressions that use keywords in this way. If there are, this should provide a good jumping off point to handle those in isolation, but this is my first time poking this parser :)

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants