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

typeof Foo['bar'] has strange precedence #19707

Open
DanielRosenwasser opened this issue Nov 3, 2017 · 6 comments
Open

typeof Foo['bar'] has strange precedence #19707

DanielRosenwasser opened this issue Nov 3, 2017 · 6 comments
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experimentation Needed Someone needs to try this out to see what happens Help Wanted You can do this Revisit An issue worth coming back to Suggestion An idea for TypeScript
Milestone

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Nov 3, 2017

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?

@DanielRosenwasser DanielRosenwasser changed the title typeof Foo['bar'] is parsed strangely typeof Foo['bar'] has strange precedence Nov 3, 2017
@mhegazy mhegazy added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Nov 3, 2017
@mhegazy
Copy link
Contributor

mhegazy commented 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.

@mhegazy mhegazy added Revisit An issue worth coming back to and removed In Discussion Not yet reached consensus labels Nov 7, 2017
@RyanCavanaugh
Copy link
Member

Seems too late to change this now.

@DanielRosenwasser
Copy link
Member Author

Worth reconsidering in my opinion #44857

I kind of doubt anyone would be broken by this change.

@fatcerberus
Copy link

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.

@sandersn sandersn added the Experimentation Needed Someone needs to try this out to see what happens label Jul 2, 2021
@sandersn sandersn reopened this Jul 2, 2021
@sandersn
Copy link
Member

sandersn commented Jul 2, 2021

Seems easy enough to make this change and compile tonnes of code to see what the rates of breaks is.

@DanielRosenwasser DanielRosenwasser added Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this labels Jul 2, 2021
@DanielRosenwasser DanielRosenwasser added this to the Backlog milestone Jul 2, 2021
@a-tarasyuk
Copy link
Contributor

TypeQuery is parsed as EntityName. Therefore, currently we can only use the following names in TypeQuery: a (Identifier), a.b (QualifiedName).

function parseTypeQuery(): TypeQueryNode {
const pos = getNodePos();
parseExpected(SyntaxKind.TypeOfKeyword);
return finishNode(factory.createTypeQueryNode(parseEntityName(/*allowReservedWords*/ true)), pos);

@DanielRosenwasser Do we need to parse it as an expression?

/cc @RyanCavanaugh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experimentation Needed Someone needs to try this out to see what happens Help Wanted You can do this Revisit An issue worth coming back to Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants