Title: Add compiler option to allow module types to be checked in non module output files · Issue #41567 · microsoft/TypeScript · GitHub
Open Graph Title: Add compiler option to allow module types to be checked in non module output files · Issue #41567 · microsoft/TypeScript
X Title: Add compiler option to allow module types to be checked in non module output files · Issue #41567 · microsoft/TypeScript
Description: Search Terms: import type, export {} Suggestion In #41513 and #41562 projects are broken by a design change to always include export {}; in the JS output if import or import type is in the TS source, regardless of whether it is in the JS...
Open Graph Description: Search Terms: import type, export {} Suggestion In #41513 and #41562 projects are broken by a design change to always include export {}; in the JS output if import or import type is in the TS sourc...
X Description: Search Terms: import type, export {} Suggestion In #41513 and #41562 projects are broken by a design change to always include export {}; in the JS output if import or import type is in the TS sourc...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/41567
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add compiler option to allow module types to be checked in non module output files","articleBody":"**Search Terms:** `import type`, `export {}`\r\n\r\n## Suggestion\r\n\r\nIn #41513 and #41562 projects are broken by a design change to always include `export {};` in the JS output if `import` or `import type` is in the TS source, regardless of whether it is in the JS output. This is a breaking change for a lot of developers and painful to fix (and by fix I mean practically get the output of our TS project to run in browsers, not necessarily adhere to ECMA spec compliance and best possible practice).\r\n\r\nHowever, `export {};` considered desirable:\r\n\r\n> You're free to suggest a new compiler option but this is the intentional behavior.\r\n> _Originally posted by @RyanCavanaugh in https://github.com/microsoft/TypeScript/issues/41513#issuecomment-727057724_\r\n\r\nPlease could we have a new compiler option to not force all files that in any way use ES modules to always have to include `export {};` in the JS output. \r\n\r\nI'm not sure of the best way for this to be applied, but the goal would be\r\n- Type checking compatible with an ESM project.\r\n- Output compatible with the current state of module support in browsers.\r\n\r\nThis could be:\r\n- A compiler option that causes `import type` to not implicitly convert output to a module, (my preference because it still avoids any output JS having `import` without `export`, which caused #38696) or \r\n- A compiler option to not add `export {};` to _all_ modules, or \r\n- A `//@directive` we add to a file to express our intent to not output a module, or\r\n- A new `reference type ...` TS-only syntax to signal that we want to use the definition but not output a module \r\n\r\nWhatever is easiest to code and causes least friction/confusion for the community.\r\n\r\n## Use Cases\r\n\r\nI have a large TS project that uses numerous web workers, a service worker and web components that load side effects.\r\n\r\nIn the first two cases including `export {};` breaks the resulting JS, as these workers are not modules and are not intended to produce module loaded JS output.\r\n\r\nIn the case of side effect web components no `export` is expected (they use `customElements.define`) so it's just wasted bytes, but it doesn't break anything. Across a project with a lot of components the many `export {};` that will never be referenced by anything adds up.\r\n\r\nIn addition during migration between different reference types it may be extremely beneficial to not strictly enforce adherence to one type or the other, at least while not in production. Any modules = all must be modules effectively makes this migration harder, even if it is a sound best practice.\r\n\r\n## Examples\r\n\r\nI have a model `MyModel.d.ts`. \r\n\r\nIn `worker.ts` I want TS to check my code against this model:\r\n\r\n```ts\r\nimport type MyModel from './MyModel';\r\nconst test: MyModel = {};\r\ntest.propertyInModel = 1; // works and has intellisense\r\n// test.propertyNotInModel = 1; throws compile error!\r\n```\r\n\r\nI want to use the JS output this with a worker in another file:\r\n\r\n```ts\r\nconst worker = new Worker('path/worker.js');\r\n```\r\n\r\nThis worked in 3.8, but fails in 4.0 due to #41562\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code (it would fix a lot of projects broken by 4.0)\r\n* [x] This wouldn't change the runtime behaviour of existing JavaScript code (it would be opt-in)\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.) (we're removing additional content that wasn't in 3.8)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals). (particularly 4, 7 and 11)\r\n","author":{"url":"https://github.com/KeithHenry","@type":"Person","name":"KeithHenry"},"datePublished":"2020-11-17T17:39:19Z","url":"https://github.com/microsoft/TypeScript/issues/41567"}
| 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:8dbf1626-499e-e036-694d-1f58fd91de2b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B9EE:2D2F2F:26F5CDD:31BBEEA:6B018735 |
| html-safe-nonce | 72afd56329de5ec7fee3d22ec16c614ac823a4f5c506bec628ba62fde4046ca8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOUVFOjJEMkYyRjoyNkY1Q0REOjMxQkJFRUE6NkIwMTg3MzUiLCJ2aXNpdG9yX2lkIjoiMTI2MTcyMzQzMDQyMDU4MDE1MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cf90ab56a6d8e1f6d1643dcb7ca85fe6faa38bc6544b295dfb06228797ef8968 |
| hovercard-subject-tag | issue:744944457 |
| 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/41567/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bb413c7278dd8ee575f1299ba6b6d6647e0992f7981e54004a2615d01db7b5dc/microsoft/TypeScript/issues/41567 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bb413c7278dd8ee575f1299ba6b6d6647e0992f7981e54004a2615d01db7b5dc/microsoft/TypeScript/issues/41567 |
| og:image:alt | Search Terms: import type, export {} Suggestion In #41513 and #41562 projects are broken by a design change to always include export {}; in the JS output if import or import type is in the TS sourc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | KeithHenry |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width