TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Fix private properties not being allowed in optional chains

Open Psychpsyo opened this issue 1 year ago • 1 comments

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 https://github.com/tc39/proposal-class-fields/pull/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:

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

Psychpsyo avatar Oct 17 '24 21:10 Psychpsyo

@microsoft-github-policy-service agree

Psychpsyo avatar Oct 17 '24 21:10 Psychpsyo

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.

Done that now.

Psychpsyo avatar Jul 25 '25 09:07 Psychpsyo