microsoft / microsoft/TypeScript

Named Tuple elements should allow reserved words as their names

Open
#45,819 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

tuple elements label reserved word
named tuple elements

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Common "Bugs" That Aren't Bugs"
⏯ Playground Link

Playground link with relevant code

💻 Code
type x = [if: any]
// but some keywords are allowed, e.g.
type y = [void: any]
🙁 Actual behavior
Unused label.
'any' only refers to a type, but is being used as a value here.
Type expected.
'(' expected.
Unexpected keyword or identifier.
Declaration or statement expected.
🙂 Expected behavior

No errors. As a label it should allow any identifiers, including reserved words.

👩‍💻 Possible solution

Although parseTupleElementNameOrTupleElementType allows any identifier as its name:

https://github.com/microsoft/TypeScript/blob/0af2497fecef9e41d7d0260fd37932dd33912c66/src/compiler/parser.ts#L3559

before it is invoked, the isListElement requires a tuple member start a type:

https://github.com/microsoft/TypeScript/blob/0af2497fecef9e41d7d0260fd37932dd33912c66/src/compiler/parser.ts#L2091

This is no longer true after we support named tuple member after 4.0, which should start with any valid identifiers / keywords. Because isListElement returns false, the parser does not parse them as tuple elements, which results to confusing parsing errors.

We can create a new ParsingContext for named tuple members, and returns token() === SyntaxKind.CommaToken || tokenIsIdentifierOrKeyword(token()) in isListElement.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/compiler/parser.ts at isListElement and parseTupleElementNameOrTupleElementType, using the linked Playground code as the reproduction. Trace how named tuple members beginning with a reserved word are classified, then add a regression test covering the example and verify it produces no parsing errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.