Repository navigation
Functions wrapped in __importDefault with allowSyntheticDefaultImports emit incorrect function calls #35420
Copy link
Copy link
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
Description
Activity
A simpler repro case would be importing
module.exports = function () { 'use strict'; return !!this; }(with esModuleInterop and synthetic imports enabled, of course) - a correct downleveling would make invoking this function with no implied receiver return false; TS’s output currently seems to make it return true.AFAICT the same bug exists for named imports.
// foo.ts export function foo() { return !!this; } // bar.ts import { foo } from './foo.js'; foo(); // transpiled bar.js var foo_js_1 = require('./foo.js'); foo_js_1.foo(); // this line should be `(0, foo_js_1.foo)()`
Jordan Harband (@ljharb) please correct me if I'm wrong.
Yes, that’s the same bug, thanks.
- added a commit that references this issue
on Dec 27, 2019 Fix is up at #35877
- addedRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
on Aug 31, 2020 - added a commit that references this issue
on Feb 9, 2021 - addedFix AvailableA PR has been opened for this issueA PR has been opened for this issue
on Feb 9, 2021 - added a commit that references this issue
on Mar 3, 2021 Ron Buckton (@rbuckton) Sorry to disturb you, but your PR seems to wrap all imported symbols with
(0, ...), but I think it's not necessary and may be skipped when the target is a "const arrow function".- added a commit that references this issue
on Sep 10, 2021 - locked as resolved and limited conversation to collaborators
on Oct 21, 2025
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
The following code:
Is transpiled into the following JS with
esModuleInterop&allowSyntheticDefaultImports:Full
tsconfig.json:(CodeSandbox.)
Executing this code leads to:
That is because the promise.allsettled package handles
thisaccording to the spec, see es-shims/Promise.allSettled#5. The author of that library is Jordan Harband (@ljharb) who will have deeper understanding of the specifics than I do. (Also, thanks for nudging me to report this issue!)The problem is most likely the emitted function call, which looks like this:
In comparison, Babel + TS emits this (which works):
Babel playground.
TypeScript Version: 3.7.2
Search Terms:
esModuleInterop,allowSyntheticDefaultImports, emit, transpile, downlevel compile, ES Modules, default, wrapper, Node.js, module.