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

Instantiation expressions not allowed in logical/coalescing expressions #49362

Closed
DanielRosenwasser opened this issue Jun 2, 2022 · 3 comments · Fixed by #49353
Closed

Instantiation expressions not allowed in logical/coalescing expressions #49362

DanielRosenwasser opened this issue Jun 2, 2022 · 3 comments · Fixed by #49353
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@DanielRosenwasser
Copy link
Member

declare let f: undefined | (<T>(x: T) => T);

let g = f<string> || ((x: string) => string);

Maybe OT Since you are fine-tuning how instantiation expressions are parsed: it's weird that a<b> ?? c is not valid code, from a syntactic point of view.

Originally posted by @nicolo-ribaudo in #49353 (comment)

@ahejlsberg
Copy link
Member

Right, for error recovery reasons we currently consider binary operators to be expression starters. We definitely shouldn't when they follow a (potential) instantiation expression.

@ahejlsberg
Copy link
Member

Also, a compelling reason to parse as an instantiation expression when the > is followed by a line break and an identifier is:

let x = foo<string>
bar(10, 20)  // Function call

Though obviously JavaScript would parse it differently.

@ahejlsberg
Copy link
Member

ahejlsberg commented Jun 3, 2022

Honestly, the more I look at this the more I think the rule should be that a potential instantiation expression immediately followed by a line break is actually considered an instantiation expression followed by ASI, regardless of the next token. Consider:

let x1 = foo<string>
this.bar()

let x2 = foo<string>
function bar() {}

let x3 = foo<string>
class C {}

let x4 = foo<string>
delete obj.x

let x5 = foo<string>
void bar()

For all of the above we currently parse the < and > as relational operators. Messy!

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants