Title: Functions wrapped in `__importDefault` with allowSyntheticDefaultImports emit incorrect function calls · Issue #35420 · microsoft/TypeScript · GitHub
Open Graph Title: Functions wrapped in `__importDefault` with allowSyntheticDefaultImports emit incorrect function calls · Issue #35420 · microsoft/TypeScript
X Title: Functions wrapped in `__importDefault` with allowSyntheticDefaultImports emit incorrect function calls · Issue #35420 · microsoft/TypeScript
Description: The following code: import allSettled from "promise.allsettled"; const resolved = Promise.resolve(42); const rejected = Promise.reject(-1); allSettled([resolved, rejected]).then(results => { console.log(results); }); Is transpiled into t...
Open Graph Description: The following code: import allSettled from "promise.allsettled"; const resolved = Promise.resolve(42); const rejected = Promise.reject(-1); allSettled([resolved, rejected]).then(results => { consol...
X Description: The following code: import allSettled from "promise.allsettled"; const resolved = Promise.resolve(42); const rejected = Promise.reject(-1); allSettled([resolved, rejected]).then(results =...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/35420
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Functions wrapped in `__importDefault` with allowSyntheticDefaultImports emit incorrect function calls","articleBody":"The following code:\r\n\r\n```ts\r\nimport allSettled from \"promise.allsettled\";\r\n\r\nconst resolved = Promise.resolve(42);\r\nconst rejected = Promise.reject(-1);\r\n\r\nallSettled([resolved, rejected]).then(results => {\r\n console.log(results);\r\n});\r\n```\r\n\r\nIs transpiled into the following JS with `esModuleInterop` & `allowSyntheticDefaultImports`:\r\n\r\n```js\r\n\"use strict\";\r\nvar __importDefault =\r\n (this && this.__importDefault) ||\r\n function(mod) {\r\n return mod && mod.__esModule ? mod : { default: mod };\r\n };\r\nObject.defineProperty(exports, \"__esModule\", { value: true });\r\nconst promise_allsettled_1 = __importDefault(require(\"promise.allsettled\"));\r\nconst resolved = Promise.resolve(42);\r\nconst rejected = Promise.reject(-1);\r\npromise_allsettled_1.default([resolved, rejected]).then(results => {\r\n console.log(results);\r\n});\r\n```\r\n\r\nFull `tsconfig.json`:\r\n\r\n```js\r\n{\r\n \"compilerOptions\": {\r\n \"target\": \"es2018\",\r\n \"module\": \"commonjs\",\r\n \"strict\": true,\r\n \"allowSyntheticDefaultImports\": true,\r\n \"esModuleInterop\": true,\r\n },\r\n \"include\": [\r\n \"src\"\r\n ]\r\n}\r\n```\r\n\r\n([CodeSandbox](https://codesandbox.io/s/inspiring-lewin-eyt6r).)\r\n\r\nExecuting this code leads to:\r\n\r\n```\r\nTypeError: #\u003cObject> is not a constructor\r\n at Object.resolve (\u003canonymous>)\r\n at Object.PromiseResolve (/sandbox/node_modules/es-abstract/es2018.js:160:10)\r\n at /sandbox/node_modules/promise.allsettled/implementation.js:26:24\r\n at Function.from (\u003canonymous>)\r\n at Object.allSettled (/sandbox/node_modules/promise.allsettled/implementation.js:19:22)\r\n at Object.allSettled [as default] (/sandbox/node_modules/promise.allsettled/index.js:16:9)\r\n at Object.\u003canonymous> (/sandbox/src/demo.ts:6:11)\r\n at Module._compile (internal/modules/cjs/loader.js:776:30)\r\n at Module.m._compile (/sandbox/node_modules/ts-node/src/index.ts:536:23)\r\n at Module._extensions..js (internal/modules/cjs/loader.js:787:10)\r\n```\r\n\r\nThat is because the promise.allsettled package handles `this` according to the spec, see https://github.com/es-shims/Promise.allSettled/issues/5. The author of that library is @ljharb who will have deeper understanding of the specifics than I do. (Also, thanks for nudging me to report this issue!)\r\n\r\nThe problem is most likely the emitted function call, which looks like this:\r\n\r\n```js\r\npromise_allsettled_1.default([resolved, rejected]).then(results => {\r\n console.log(results);\r\n});\r\n```\r\n\r\nIn comparison, Babel + TS emits this (which works):\r\n\r\n```js\r\n(0, _promise.default)([resolved, rejected]).then(function (results) {\r\n console.log(results);\r\n});\r\n```\r\n\r\n[Babel playground](https://babeljs.io/en/repl#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=JYWwDg9gTgLgBAQwDZIMoFMYyegJnAMyghDgHIxiRgBndAOmSTqx1zIG4AoLgYwgB2NeFHQ0ISAG544AXjgAFKrQajxU9AAoALACYAlN35CR6AFbpeMGfKUkV9URauaAtAEZDPJhlZ5NANpqEtK4ADRwTpbWuAC6-vQwABboAppqAK5IMDRyAHxwAN5ccHDG6gxIEADm6WJZOV4AvoZAA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2%2Ctypescript&prettier=false&targets=&version=7.7.4&externalPlugins=).\r\n\r\n**TypeScript Version:** 3.7.2\r\n\r\n**Search Terms:** `esModuleInterop`, `allowSyntheticDefaultImports`, emit, transpile, downlevel compile, ES Modules, default, wrapper, Node.js, module.","author":{"url":"https://github.com/borekb","@type":"Person","name":"borekb"},"datePublished":"2019-11-29T10:31:28Z","url":"https://github.com/microsoft/TypeScript/issues/35420"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0ae107b8-a3bc-0935-3f90-cb2bd21ca265 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C150:10B16D:118B49C:1762175:6AED5946 |
| html-safe-nonce | b96287e5b07d6bf3a5af9775189e8ae9895bf25b0e1bb60eecf1b497134430ae |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTUwOjEwQjE2RDoxMThCNDlDOjE3NjIxNzU6NkFFRDU5NDYiLCJ2aXNpdG9yX2lkIjoiMTU5ODMzMDk3ODE2MzMxNzA2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a6d55065cabdb41c42973fa5c47d8e8ee0a2e33416c74172e77fbdf1e5dc2d72 |
| hovercard-subject-tag | issue:530272338 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/35420/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fa378ad4775c13acce1741f3399fd9b7eb386296ef14edb0965983361496230a/microsoft/TypeScript/issues/35420 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fa378ad4775c13acce1741f3399fd9b7eb386296ef14edb0965983361496230a/microsoft/TypeScript/issues/35420 |
| og:image:alt | The following code: import allSettled from "promise.allsettled"; const resolved = Promise.resolve(42); const rejected = Promise.reject(-1); allSettled([resolved, rejected]).then(results => { consol... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | borekb |
| hostname | github.com |
| expected-hostname | github.com |
| None | 56fb42677ae87ed3e64410323d67980aa65d0485568a7858f874e7edfc6d1c11 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c9a37e2423ed382cf39f70b5aa4b4858b40a3b0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width