Skip to content

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

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

Ryan Cavanaugh (RyanCavanaugh) wants to merge 4 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 Nov 30, 2026

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

2 participants