Skip to content

Allow private identifiers in optional chains and lower them correctly - #65743

Draft
Ryan Cavanaugh (RyanCavanaugh) wants to merge 5 commits into
mainfrom
copilot/fix-ts18030-private-identifiers
Draft

Ryan Cavanaugh (RyanCavanaugh) wants to merge 5 commits into
mainfrom
copilot/fix-ts18030-private-identifiers

Conversation

@RyanCavanaugh

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

a?.#p is valid ES2022, but the parser reports TS18030 on it. When private names are lowered (targets below ES2022, or useDefineForClassFields: false), the class fields transform also dropped the short-circuit: a?.#p became __classPrivateFieldGet(a, _A_p, "f"), which throws when a is nullish. At ES2020/ES2021 the ?. itself is kept, so the class fields transform has to handle the whole chain itself.

class A {
    #p = 1;
    #m() { return this; }
    test(a?: A) {
        a?.#p;     // a === null || a === void 0 ? void 0 : __classPrivateFieldGet(a, _A_p, "f")
        a?.#m();   // ... : __classPrivateFieldGet(a, _A_instances, "m", _A_m).call(a)
        a?.#m?.(); // (a === null || a === void 0 ? void 0 : __classPrivateFieldGet(...))?.call(a)
    }
}

Parser

  • Remove the TS18030 check in parsePropertyAccessExpressionRest. The checker already handles these chains.

Class fields transform

  • A chain run containing a transformed private name is lowered to x === null || x === void 0 ? void 0 : …. A run is the links from the outermost node back to its ?.. Only runs that contain the private name are lowered; a?.#p?.b becomes (…)?.b and is left to the ES2020 transform.
  • this binding: a lowered private-name link followed by a call captures its receiver and emits .call(receiver). A receiver that isn't simple to copy goes into a temp. This covers:
    • calls outside the chain: a?.#m?.(), (a?.#m)(), (a?.#m)!()
    • parenthesized and super receivers at the start of the chain
  • Tagged templates: (a?.#m)!`x` becomes (…).bind(a) followed by the template.
  • delete: delete a?.#p.x becomes a === null || a === void 0 ? true : delete __classPrivateFieldGet(a, _A_p, "f").x.
  • Mixed chains: chains that mix lowered and native private names (static privates in an ES-decorated class at ES2022) are handled.

Tests

  • New compiler tests: privateNameOptionalChain{,Call,Delete,Static,Decorators,Errors}.ts, at es2019/es2020/es2022 (Decorators and Errors at es2019/es2022 only).
  • privateIdentifierChain.1: TS18030 is gone; TS2532 remains.
  • privateNameUncheckedJsOptionalChain: this?.#foo now types as error, matching this.#foo in unchecked JS. The .errors.txt baseline is removed.

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug and removed For Milestone Bug PRs that fix a bug with a specific milestone labels Nov 30, 2026
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) changed the title [WIP] Fix TS18030 crash due to optional chain with private identifiers Allow private identifiers in optional chains and lower them correctly Nov 30, 2026
@RyanCavanaugh

Copy link
Copy Markdown
Member Author

Copilot you're duplicating code here with the es2020 transform, try to refactor this so it's not so duplicative

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

Copilot AI commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Copilot you're duplicating code here with the es2020 transform, try to refactor this so it's not so duplicative

Done in 7eb94d9. The ES2020 lowering engine is now optionalChainLowering in optionalchain.go, and both transforms use it. The class fields transform plugs in its existing accessPrivateIdentifier/createPrivateIdentifierAccessHelper and keeps only the check for which chains contain a transformed private name; its copy of the lowering logic is gone (net −70 lines).

No existing baselines changed. One ES2020 emit change has no test coverage: (a.b!)?.() now emits _a.call(a) instead of _a(), which used to lose this. Lowered private-name chains give the same results as native esnext when run under Node, at es2015 through es2021 and with useDefineForClassFields: false.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TS18030: An optional chain cannot contain private identifiers

2 participants