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
Type queries combined with indexed access types currently produce a parse tree that is surprising in its behavior.
One would think that typeof Foo['bar'] would be parsed as typeof (Foo['bar']), which would really be something like typeof Foo.bar.
That's not the case. It's actually parsed as (typeof Foo)['bar'].
Conveniently, it seems that semantically (when type-checking) these are identical, but it seems strange for syntactic consumers. Do we believe this is currently working as intended?
It should have the same precedence as in an expression. but this is a breaking change, and so far there is no functional impact of this on the output. marking it as a revisit for now.
I remember discovering this a while back and was quite surprised that the relative precedence for typeof vs. [] in a type context was the exact opposite of runtime typeof.
Worth noting though that I’ve seen a lot of examples given out on Gitter and Discord that freely do stuff like typeof Class["prop"], which is how I found out about the odd precedence in the first place. I prefer to add parentheses to minimize confusion, but from what I’ve seen, it is likely to be a breaking change if the precedence is changed now.
Activity
[-]typeof Foo['bar'] is parsed strangely[/-][+]typeof Foo['bar'] has strange precedence[/+]mhegazy commentedon Nov 7, 2017
It should have the same precedence as in an expression. but this is a breaking change, and so far there is no functional impact of this on the output. marking it as a revisit for now.
RyanCavanaugh commentedon Jun 23, 2021
Seems too late to change this now.
DanielRosenwasser commentedon Jul 1, 2021
Worth reconsidering in my opinion #44857
I kind of doubt anyone would be broken by this change.
fatcerberus commentedon Jul 1, 2021
I remember discovering this a while back and was quite surprised that the relative precedence for
typeofvs.[]in a type context was the exact opposite of runtimetypeof.Worth noting though that I’ve seen a lot of examples given out on Gitter and Discord that freely do stuff like
typeof Class["prop"], which is how I found out about the odd precedence in the first place. I prefer to add parentheses to minimize confusion, but from what I’ve seen, it is likely to be a breaking change if the precedence is changed now.sandersn commentedon Jul 2, 2021
Seems easy enough to make this change and compile tonnes of code to see what the rates of breaks is.
a-tarasyuk commentedon Jan 21, 2022
TypeQuery is parsed as
EntityName. Therefore, currently we can only use the following names in TypeQuery:a(Identifier),a.b(QualifiedName).TypeScript/src/compiler/parser.ts
Lines 3078 to 3081 in ef9fd97
Daniel Rosenwasser (@DanielRosenwasser) Do we need to parse it as an expression?
/cc Ryan Cavanaugh (@RyanCavanaugh)