Skip to content

Fix private properties not being allowed in optional chains #60263

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Psychpsyo
Copy link
Contributor

@Psychpsyo Psychpsyo commented Oct 17, 2024

Fixes #42734

This makes it so that expressions of the form foo?.#bar or foo?.bar.#baz are no longer seen as invalid, since the relevant JavaScript standards allow these by now and TypeScript claims to be a superset of JavaScript.
Specifically, private properties are now allowed inside an optional chain.
This syntax is allowed since tc39/proposal-class-fields#301

The added this.a = this; in privateIdentifierChain.1.ts is there so that line 14, this?.a.#b;, does not error because a might be undefined, as that's not what this file is testing for.

I'm sorry for fixing an issue that is not in the backlog, but this is bothering me personally and has existed for years at this point.
Otherwise, I have verified that:

  • Code is up-to-date with the main branch
  • You've successfully run hereby runtests locally
  • There are new or updated unit tests validating the change

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Oct 17, 2024
@Psychpsyo
Copy link
Contributor Author

@microsoft-github-policy-service agree

@Psychpsyo Psychpsyo changed the title Fix private properties not ebing allowed in optional chains Fix private properties not being allowed in optional chains Oct 17, 2024
@jakebailey jakebailey requested a review from rbuckton January 21, 2025 23:13
Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming back to this, this PR is missing a few things that would make this work:

  • A test that the downlevelling works, so set the test's target to @target: esnext,es2015.
  • A new test that verifies some sort of cross-class error happens, e.g. copy and paste the class in the test to B, and there should be type errors since A is the wrong class.

@github-project-automation github-project-automation bot moved this from Not started to Waiting on author in PR Backlog Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Waiting on author
Development

Successfully merging this pull request may close these issues.

TS18030: An optional chain cannot contain private identifiers
4 participants