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

Binary operator after TypeScript as assertion causes wrong SyntaxError #11446

Closed
woongsikchoi opened this issue Apr 20, 2020 · 5 comments · Fixed by #11912
Closed

Binary operator after TypeScript as assertion causes wrong SyntaxError #11446

woongsikchoi opened this issue Apr 20, 2020 · 5 comments · Fixed by #11912
Assignees
Labels
area: typescript i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@woongsikchoi
Copy link

Bug Report

Current Behavior

If a bianry operator like >= appears directly after TypeScript as assertion, a wrong SyntaxError occurs.

Input Code

function foo(x: any) {
	return x as number >= 0;
}

The above code causes the following SyntaxError:

{ SyntaxError: test.ts: Unexpected token (2:22)
  1 | function foo(x: any) {
> 2 |   return x as number >= 0;
    |                       ^
  3 | }
  4 | 

Expected behavior/code

No SyntaxError should occur.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: .babelrc
{
  "presets": ["@babel/preset-typescript"]
}

Environment

  System:
    OS: Linux 4.4 Ubuntu 14.04.5 LTS, Trusty Tahr
  Binaries:
    Node: 10.15.1 - ~/local/lib/node-v10.15.1-linux-x64/bin/node
    Yarn: 1.22.4 - ~/local/lib/node-v10.15.1-linux-x64/bin/yarn
    npm: 6.9.0 - ~/local/lib/node-v10.15.1-linux-x64/bin/npm
  npmPackages:
    @babel/cli: ^7.8.4 => 7.8.4 
    @babel/core: ^7.9.0 => 7.9.0 
    @babel/preset-typescript: ^7.9.0 => 7.9.0 
@babel-bot
Copy link
Collaborator

Hey @woongsikchoi! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Apr 20, 2020

@DanielRosenwasser What is the precedence of the as operator? It seems like it has very different precedences on the left and on the right, but I'm not sure about it.

Specifically, on the right (EDIT: I initially wrote left here) is it higher or lower than relational operators? I'm asking because from my experiments it has an higher precedence than > and lower precedence than <, but > and < should have the same precedence.

a as b > c > +d; // This is parsed as two `>` operators, and an unary `+`
a as b < c > +d; // This is parsed as a sum between (a as b<c>) and d

Also, is it correct that on the left it is between relational operators and equality operators?

@woongsikchoi
Copy link
Author

The same problem occurs also for the nullish coalescing operator ??.

@ghost
Copy link

ghost commented Aug 3, 2020

Also dealing with this bug. JSON.parse(value) as unknown ?? undefined throws a syntax error.

@JLHwung
Copy link
Contributor

JLHwung commented Aug 3, 2020

The same problem occurs also for the nullish coalescing operator ??.

Can you open a new issue? They have different root causes. @woongsikchoi

Never mind I have fixed both of them.

@JLHwung JLHwung self-assigned this Aug 4, 2020
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Nov 4, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants