Skip to content

CommonJS emit gives an imported function the wrong this when the callee is wrapped #65414

Open

Description

@trackoor

🔎 Search Terms

commonjs emit parenthesized call this, (0, a_1.f), indirect call wrapped callee, satisfies call this

🕗 Version & Regression Information

Not a regression.

  • Fails: 5.9.3, 6.0.3, 7.0.2, 7.1.0-dev.20261106.1, 7.1.0-dev.20261109.1

⏯ Playground Link

https://www.typescriptlang.org/play/v2/?ts=6.0.3#code/v2/N4IghgxgLglgbgUwGIwDYJALhAegO4D2ATgNYDOADpAjmAHRRkgA0IAZmgk5qPseVQg1GEAgDsOAczoArMuKwhgAHTEACNcpCiAthU5EA8hVjiyWzGpXqNmkGShEY0C2scBXBM1W27YggCSeqjOMFAASghQ7kRi5lhuRJ7eNhpaYKioBHgAqmJECJAAFmAARugAwgQAJgiubBlkXj626ZnZee5N1QAyZQio8ZYNg82pdlwAsjXu6AFiUAhEBBSuHmO+WrUQqGBEYKZia0kbrSA6M+iuWroXYnJaKZvnlwiR8qjuh9cg-rWPLTSICge0kUR+XAATAAGSGQgHjLRyAAePwKkCgCOeXQQABEEBwxMhiBVdmQyCgBtUhmoRk0nmcYJJ-AV8RQChADjAzD8AGx0aFaFoAX1UorELFwhFIlGotAY3BACGRFGIUFp7jE0G56jYAAooEUYGRLJqSP48GJmGo6LaAPqgk1qM0WsQAbQAugBKKxqArRWJuI1kADcanFkr4MsENBgYlqyIVihgejVvrY4dpyx0djotC0IdUUAAnhQEGokGoALxqPU+qsAPmdYnN2TEhaOYhwODUAFEVQhoAhqpYEIgiMW1CEiWp2XHGIlPLWwOoU6qiItqhqtYc1Ma1JzMsO1HgwkUg-uq1fm7VCcOvXRVKI4gR0HQspI9Vp9T7fH+gdaP7Vtemq3nG95hv+UFqN2agIDoYSWHq0LWmAdoAIx0GwXp1k+Zivgg74EJ+Wh6thdbQQBtbkfWIHxgS4HVF6kGURosHwYhy4YVhOFenhL5vh+X4gGRahgGQFa8X+jzUWJElIFJV41qBDFEkxLF-uxCFQJYaGYT+-EfIRQmkWwACEUnQTJZEWRRSk3qpEGsWxPYcTpXH6bxhkEURJEiRmZBcmQHBcJJuEsLJgWwMFMChQpdl0WBanMTBrnabp3EGVq+GCcRwlkV6AAGyKFZR1nYcVpX2Spd7qc5qVwelHk8WolWqCAwqsE06DagoPAgKqZBhDqVSfDoEqYOhrCDcN4g9OBABy7g6KUSxYFN9gDIOhwAMoghuo3LRNG3ddtOp7XsUDzUSS0rWtk2dSA45DX1kLCkAA

(Playground v2, 6.0.3, module: commonjs; press Run.)

💻 Code

// a.ts
export function f(this: unknown) { return this; }

// index.ts
import { f } from "./a";
type F = () => unknown;
console.log(f() === undefined);                 // true   emit: (0, a_1.f)()
console.log((f)() === undefined);               // false  emit: (a_1.f)()
console.log((f as F)() === undefined);          // false  emit: a_1.f()
console.log((f!)() === undefined);              // false  emit: (a_1.f)()
console.log((f satisfies F)() === undefined);   // false  emit: a_1.f()
console.log((f)`x` === undefined);              // false  emit: (a_1.f) `x`

🙁 Actual behavior

Only the bare call f() is emitted as (0, a_1.f)(). When the callee is parenthesized or wrapped in as, <F>, ! or satisfies, the emit calls a_1.f as a method, so this is the exports object instead of undefined.

🙂 Expected behavior

All six calls print true, as in ESM output: the wrapped forms should also be emitted as (0, a_1.f)(...).


This was found with an AI-assisted differential testing tool; the repros and outputs above were re-run on the listed versions.

Activity

  1. sundeep8967 commented on Nov 14, 2026

    @sundeep8967

    Hi trackoor, I can reproduce this issue where wrapping an imported callee in parenthesized expressions, type assertions (as, <T>), non-null assertions (!), or satisfies skips the (0, a_1.f)() indirect call emit in the CommonJS/AMD emitter, binding this to the module exports object.

    I would like to work on this and prepare a PR with regression tests to ensure the indirect call helper unwraps type-assertion and parenthesized expressions before checking whether to emit (0, ...). Could you please assign this to me?

  2. added
    Bug
    Domain: JS Emit
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDomain: JS Emit

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions