Skip to content

Preserve this in downleveled optional chains wrapped in erased type syntax or used as template tags - #65610

Draft
Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
mainfrom
copilot/fix-optional-chaining-issue
Draft

Ryan Cavanaugh (RyanCavanaugh) wants to merge 3 commits into
mainfrom
copilot/fix-optional-chaining-issue

Conversation

@RyanCavanaugh

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

Copy link
Copy Markdown
Member

When optional chaining is downleveled (target < ES2020), some calls and template tags lose their receiver, so the method runs with this === undefined. Affected callees:

  • an optional chain wrapped in parentheses together with !, as, satisfies, <T> or type arguments;
  • super.fn! and super.fn as any, as in (super.fn!)?.();
  • any parenthesized optional chain used as a template tag.
(obj?.fn!)?.();
(obj?.fn as any)();
(obj.fn<string>)?.();
(obj?.fn)`x`;

Cause

The type eraser runs first and leaves PartiallyEmittedExpression (for !/as/satisfies/<T>) and ExpressionWithTypeArguments (for instantiation expressions) where the source had parentheses. The optional-chain transform only looked through ParenthesizedExpression when capturing this, and it did not handle tagged templates at all.

Changes (estransforms/optionalchain.go)

  • Outer expressions: visitParenthesizedExpression becomes visitOuterExpression. It looks through parentheses, PartiallyEmittedExpression and ExpressionWithTypeArguments and re-wraps the captured this reference.
  • Calls: a call whose callee is an optional chain under any outer-expression wrapper (OEKAll) now emits .call(thisArg). Previously only bare parentheses did. This also fixes the unparenthesized obj.gen<string>?.().
  • Tagged templates: a tag that is a wrapped optional chain becomes target.bind(thisArg), which is how the class-fields transform handles private-name tags. Using .bind keeps the template-strings object as the first argument, so its identity is preserved.
// (obj?.fn as any)()
(obj === null || obj === void 0 ? void 0 : obj.fn)()          // before
(obj === null || obj === void 0 ? void 0 : obj.fn).call(obj)  // after

// (obj?.fn)`x`
(obj === null || obj === void 0 ? void 0 : obj.fn) `x`           // before
(obj === null || obj === void 0 ? void 0 : obj.fn).bind(obj) `x` // after

Not changed

delete (obj?.x!) and similar wrapped forms keep their current emit. The checker already rejects them with TS2703, and 6.0 emits them the same way.

Tests

New compiler test optionalChainThisArgWithOuterExpressions.ts, run at es2019 and es2020. It covers every shape from the issue plus element access, nested chains and super. The first commit adds the test with the old output, so the es2019 baseline diff shows the change; es2020 output is unchanged.

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

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) changed the title [WIP] Fix downlevel optional chaining losing this for parenthesized callees Preserve this in downleveled optional chains wrapped in erased type syntax or used as template tags Nov 20, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Downlevel optional chaining loses this for parenthesized callees with !, as or type arguments, and for parenthesized template tags

2 participants