You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot define a union type with a new signature type #4067
Parser errors are best-effort because the fact that there's a parser error means we're really not sure what's going on or what the intended code was. The earliest error is "type expected", which is nearly (?) correct. I'm not sure what specifically we could do; maybe Cyrus Najmabadi (@CyrusNajmabadi) has some ideas.
we could technically parse a wider range of values when parsing the union type. We could then report an error afterwards with more detail. I wouldn't have a problem with that at all.
Note: it's necessary for precedence that we disallow this. Otherwise there's far too much ambiguity. For example, it would then get very tricky to have a rule to properly parse this:
a: string|new()=>B|C
So it's better to use precedence to outright disallow call/construct signatures when parsing a union/intersection type and give the escape hatch of parenthesizing things if that's what you actually want.
Hit this recently with intersection types. Patch (was a bit tricky) uses this message : Invalid type. To avoid ambiguity, add parentheses: '(new() => void)'
Activity
RyanCavanaugh commentedon Jul 29, 2015
Due to parsing precedence, you have to write
tinganho commentedon Jul 29, 2015
Ok thanks, shouldn't it give me some kind of feedback on that?
RyanCavanaugh commentedon Jul 29, 2015
Parser errors are best-effort because the fact that there's a parser error means we're really not sure what's going on or what the intended code was. The earliest error is "type expected", which is nearly (?) correct. I'm not sure what specifically we could do; maybe Cyrus Najmabadi (@CyrusNajmabadi) has some ideas.
CyrusNajmabadi commentedon Jul 29, 2015
we could technically parse a wider range of values when parsing the union type. We could then report an error afterwards with more detail. I wouldn't have a problem with that at all.
CyrusNajmabadi commentedon Jul 29, 2015
Note: it's necessary for precedence that we disallow this. Otherwise there's far too much ambiguity. For example, it would then get very tricky to have a rule to properly parse this:
So it's better to use precedence to outright disallow call/construct signatures when parsing a union/intersection type and give the escape hatch of parenthesizing things if that's what you actually want.
DanielRosenwasser commentedon Jul 29, 2015
I'd say that it's advantageous to users to disallow this, since we force writers to be explicit for future readers.
But yes, I think parsing this and reporting an error would be helpful, such as:
or the following two separately:
benliddicott commentedon Sep 7, 2015
Maybe just add: "(Tip: Do you need parentheses?)"
jbondc commentedon Sep 20, 2015
Hit this recently with intersection types. Patch (was a bit tricky) uses this message :
Invalid type. To avoid ambiguity, add parentheses: '(new() => void)'Andarist commentedon Aug 1, 2023
Today this is reported nicely:
This matches Daniel's suggestion from the comment here so the issue can be closed, cc Jake Bailey (@jakebailey)