Fix private properties not being allowed in optional chains
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
mainbranch - [x] You've successfully run
hereby runtestslocally - [x] There are new or updated unit tests validating the change
@microsoft-github-policy-service agree
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.