Title: Hoisted function declarations should be assigned to `exports` before any `require` calls · Issue #39853 · microsoft/TypeScript · GitHub
Open Graph Title: Hoisted function declarations should be assigned to `exports` before any `require` calls · Issue #39853 · microsoft/TypeScript
X Title: Hoisted function declarations should be assigned to `exports` before any `require` calls · Issue #39853 · microsoft/TypeScript
Description: TypeScript Version: 4.2.0-beta Search Terms: hoisted function exports hoisted function export hoisted function export hoist function Code // @module: CommonJS // @showEmit // @showEmittedFile: b.js // @filename: a.ts import { b } from "....
Open Graph Description: TypeScript Version: 4.2.0-beta Search Terms: hoisted function exports hoisted function export hoisted function export hoist function Code // @module: CommonJS // @showEmit // @showEmittedFile: b.js...
X Description: TypeScript Version: 4.2.0-beta Search Terms: hoisted function exports hoisted function export hoisted function export hoist function Code // @module: CommonJS // @showEmit // @showEmittedFile: b.js...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/39853
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Hoisted function declarations should be assigned to `exports` before any `require` calls","articleBody":"**TypeScript Version:** 4.2.0-beta\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\n\r\n- hoisted function\r\n- exports hoisted function\r\n- export hoisted function\r\n- export hoist function\r\n\r\n**Code**\r\n\r\n```ts repro\r\n// @module: CommonJS\r\n\r\n// @showEmit\r\n// @showEmittedFile: b.js\r\n\r\n// @filename: a.ts\r\nimport { b } from \"./b.js\";\r\n\r\nexport function a() {\r\n\treturn b();\r\n}\r\n\r\n// @filename: b.ts\r\nimport { a } from \"./a.js\";\r\n\r\n// Crashes when transpiled by TypeScript:\r\na();\r\n\r\nexport function b() {\r\n\treturn 123;\r\n}\r\n```\r\n\r\n[Workbench Repro](https://www.typescriptlang.org/dev/bug-workbench/?#code/PTAEAEFsHsBMFcA2BTAXKAwtSMB2ApAZQFgAoMkCAZwAtoB3AUUgEsAXCscWh59t5LABiLFOgBGAOgBWVMpwgAzUclwBDSGlBrJbOaRaQADtABObUAG9Q40AF9Qi09lAAiScCmzXAbnmlkAA8Tc0d4XABjNhZoXG0ACgBKKzIASFNkNnhTOPEkv1I7f0pwZRR1TQldfUMQi2s1e0dnSDcPHW8ChQxTNVpkKlB6GlVQNl7cKiMVWBsAT1AAFTmjZEII0xYjNlQyNXz-ILqwyOjYmySU0nTM7LiARgAmAGYCotIgA)\r\n\r\n**Expected behavior:**\r\n\r\nThe `exports.*` assignment should be done immediately after `__esModule`, just like how Babel does it.\r\n\r\n```js\r\n// a.js\r\n\"use strict\";\r\nObject.defineProperty(exports, \"__esModule\", { value: true });\r\nexports.a = a;\r\nconst b_1 = require(\"./b.js\");\r\nfunction a() {\r\n return b_1.b();\r\n}\r\n\r\n// b.js\r\n\"use strict\";\r\nObject.defineProperty(exports, \"__esModule\", { value: true });\r\nexports.b = b;\r\nconst a_js_1 = require(\"./a.js\");\r\n// Crashes when transpiled by TypeScript:\r\na_js_1.a();\r\nfunction b() {\r\n return 123;\r\n}\r\n```\r\n\r\n**Actual behavior:**\r\n\r\nThe exports assignment is performed after the function declaration, which causes behaviour differences between native **ES** modules (where hoisted function declarations are initialised before any code begins executing) and transpiled **CommonJS** modules (where hoisted function declarations are added to the `exports` object after `require(…)` calls)\r\n\r\n```js\r\n// a.js\r\n\"use strict\";\r\nObject.defineProperty(exports, \"__esModule\", { value: true });\r\nexports.a = void 0;\r\nconst b_1 = require(\"./b.js\");\r\nfunction a() {\r\n return b_1.b();\r\n}\r\nexports.a = a;\r\n\r\n// b.js\r\n\"use strict\";\r\nObject.defineProperty(exports, \"__esModule\", { value: true });\r\nexports.b = void 0;\r\nconst a_js_1 = require(\"./a.js\");\r\n// Crashes when transpiled by TypeScript:\r\na_js_1.a();\r\nfunction b() {\r\n return 123;\r\n}\r\nexports.b = b;\r\n```\r\n\r\n**Playground Link:** [a.js](https://www.typescriptlang.org/play?module=1&ts=4.2.0-beta&useJavaScript=true#code/JYWwDg9gTgLgBAbzgIzgXzgMyhEcBEAdAPTKEBWAzvgNwBQdApgB6SxYCuAdgMYzAQucAIYAKAJSI6cGXCiMYHKEOQT6aIA), [b.js](https://www.typescriptlang.org/play?module=1&ts=4.2.0-beta&useJavaScript=true#code/JYWwDg9gTgLgBAbzgQzgXzgMyhEcBEAdAPTKEBWAzvgNwBQdxxcAwlMpQBYCmlcA7jwB2cGOyGUwwADbcAJnABGATzgAVZWG4BlAMZRgYGAC46yABQBKenW4APSLCwBXIbpjAIIxVcR04AXBQ3DDOUCIAjABMAMz0aEA)\r\n\r\n**Related Issues:** \u003chttps://github.com/microsoft/TypeScript/pull/37093>\r\n","author":{"url":"https://github.com/ExE-Boss","@type":"Person","name":"ExE-Boss"},"datePublished":"2020-07-31T20:20:49Z","url":"https://github.com/microsoft/TypeScript/issues/39853"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:2dc1139a-d972-2cca-8d2f-776d833f31d2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E7BE:E6B97:1790E54:2077506:6B0588EB |
| html-safe-nonce | ab53a474468b365a814c16c30c41abe7247fee9c449965a28ca863e4f1c1862b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFN0JFOkU2Qjk3OjE3OTBFNTQ6MjA3NzUwNjo2QjA1ODhFQiIsInZpc2l0b3JfaWQiOiIxMzYyNTM1MzIzNjk5MzQ5NzM5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | efa504217992f57d34230e79c597c853c7d445325e7823c18486956b09432cfd |
| hovercard-subject-tag | issue:670188825 |
| 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/39853/issue_layout |
| twitter:image | https://opengraph.githubassets.com/03eca82c8c252d96d73caa8dc3e511adf2b71cc8f27c2437a109bcdd1227262a/microsoft/TypeScript/issues/39853 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/03eca82c8c252d96d73caa8dc3e511adf2b71cc8f27c2437a109bcdd1227262a/microsoft/TypeScript/issues/39853 |
| og:image:alt | TypeScript Version: 4.2.0-beta Search Terms: hoisted function exports hoisted function export hoisted function export hoist function Code // @module: CommonJS // @showEmit // @showEmittedFile: b.js... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ExE-Boss |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9b4256ac5f3e310e7945ef5b71c9cb9e264357b1eb20e91d369ed6502a4c486c |
| 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 | f089e689ad324655ac44bf6cdbc04d976105b374 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width