-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Short circuit operator breaks TSC #25897
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
Comments
There's a parse error and that's why it's not emitting correctly. The specification doesn't expect a plain arrow function to follow a Here's what I get in Firefox:
Here's what I get in Chrome:
|
But Firefox gave a good error message. We should too. |
I respect your authority but this is plainly wrong. |
Can you show me what in https://tc39.github.io/ecma262/ indicates that this behavior is wrong? |
@DanielRosenwasser is correct. If you surround the arrow function in parentheses, it compiles fine. This is just an operator precedence issue; without parentheses, it is parsed as |
Thanks for the ask. Two thoughts on this:
So from the perspective of a human, or of that document, in my eyes, the argument I've put forth initially, that || should behave agnostically to closures versus lambads, does hold up. Thank you for your time in any case. PS: By the way I love this compiler. |
@LiterallyDoge I just want to point out that this it doesn't have to do with the types of the operands (you could put your function in a var or wrap it in parens and || will do as you expect modulo type errors). This is how JS syntax works. Per the specification, a LogicalORExpression's right-hand operand is a LogicalAndExpression. The only way to get an arrow function is to parenthesize it. This is true for any alternative of AssignmentExpression, e.g. you can't do |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: This is a compiler syntax issue across all versions.
Search Terms:
short circuit arrow function
Code
Expected behavior:
The short-circuit operator should be agnostic to which type of function follows it (arrow versus traditional).
Actual behavior:
Traditional functions resolve as expected, but arrow functions compile malformed: "();"
Playground Link: https://www.typescriptlang.org/play/#src=class%20Greeter%20%7B%0D%0A%20%20%20%20greeting%3A%20string%3B%0D%0A%20%20%20%20constructor(message%3A%20string)%20%7B%0D%0A%20%20%20%20%20%20%20%20this.greeting%20%3D%20message%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20greet()%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20%22Hello%2C%20%22%20%2B%20this.greeting%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Alet%20greeter%20%3D%20new%20Greeter(%22world%22)%3B%0D%0A%0D%0Alet%20button%20%3D%20document.createElement('button')%3B%0D%0Abutton.textContent%20%3D%20%22Say%20Hello%22%3B%0D%0Abutton.onclick%20%3D%20function()%20%7B%0D%0A%20%20%20%20alert(greeter.greet())%3B%0D%0A%7D%0D%0A%0D%0Avar%20functionHandler%20%3D%20%7B%0D%0A%20%20%20%20handleIt%3A%20()%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20console.log(%22HANDLE%20IT%20MAN!%20%20HANDLE%20IT!%22)%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%3B%0D%0A%0D%0Avar%20nextFunctionHandler%20%3D%20%7B%0D%0A%20%20%20%20handleIt%3A%20null%0D%0A%7D%3B%0D%0A%0D%0AnextFunctionHandler.handleIt%20%3D%20functionHandler.handleIt%20%7C%7C%20()%20%3D%3E%20%7B%0D%0A%20%20%20%20console.log(%22We%20didn't%20handle%20it.%22)%3B%0D%0A%7D%3B%0D%0A%0D%0Avar%20justSomeJavascript%20%3D%20function%20()%20%7B%0D%0A%20%20%20%20%2F%2F%20haha%0D%0A%7D%3B%0D%0A%0D%0Avar%20justSomeJavascript%20%3D%20()%20%3D%3E%20%7B%0D%0A%20%20%20%20%2F%2F%20hah%0D%0A%7D%3B%0D%0A%0D%0A%0D%0A%0D%0Adocument.body.appendChild(button)%3B
Related Issues: I didn't.
Thanks in advance.
The text was updated successfully, but these errors were encountered: