Title: [Feature Request] Add compilerOption that validates ".js" extension is present in import path · Issue #52204 · microsoft/TypeScript · GitHub
Open Graph Title: [Feature Request] Add compilerOption that validates ".js" extension is present in import path · Issue #52204 · microsoft/TypeScript
X Title: [Feature Request] Add compilerOption that validates ".js" extension is present in import path · Issue #52204 · microsoft/TypeScript
Description: Suggestion Previously, the TypeScript team have made it clear that the compiler will not modify/rewrite import path extensions to convert TypeScript imports to valid ESModule imports - that's totally fine, this is not that discussion Cur...
Open Graph Description: Suggestion Previously, the TypeScript team have made it clear that the compiler will not modify/rewrite import path extensions to convert TypeScript imports to valid ESModule imports - that's total...
X Description: Suggestion Previously, the TypeScript team have made it clear that the compiler will not modify/rewrite import path extensions to convert TypeScript imports to valid ESModule imports - that's t...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/52204
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Feature Request] Add compilerOption that validates \".js\" extension is present in import path","articleBody":"# Suggestion\r\n\r\nPreviously, the TypeScript team have [made it clear](https://github.com/microsoft/TypeScript/issues/16577) that the compiler will not modify/rewrite import path extensions to convert TypeScript imports to valid ESModule imports - that's totally fine, this is not that discussion \r\n\r\nCurrently, the TypeScript compiler does accept import paths to TypeScript source files with `.js` extensions. Despite the import saying `.js` it will resolve the relevant `.ts` file:\r\n\r\n```typescript\r\nimport { foo } from \"./foo.js\"\r\n// tsc will resolve \"./foo.ts\" but emit \"./foo.js\"\r\n```\r\n\r\nCan the TypeScript compiler include the capacity to enforce that extensions are present - (only a suggestion) perhaps via a new `moduleResolution` which respects the ES Module standard?\r\n\r\nPerhaps:\r\n\r\n```json\r\n{\r\n \"compilerOptions\": {\r\n \"moduleResolution\": \"ESNext\",\r\n \"moduleExtension\": \".js | .mjs\", // currently only .js is supported\r\n }\r\n}\r\n```\r\n\r\nWhere, when enabled:\r\n\r\n```typescript\r\n// File import\r\nimport { foo } from \"./foo\" // Invalid - compiler throws\r\nimport { foo } from \"./foo.js\" // Valid\r\n\r\n// Folder import\r\nimport { bar } from \"./bar\" // Invalid - compiler throws\r\nimport { bar } from \"./bar/index.js\" // Valid\r\n```\r\n\r\nPros:\r\n- This will help with the creation of JavaScript packages that can be used directly in the browser without a bundler\r\n\r\nCons:\r\n- Aforementioned libraries would need to ensure that all of their dependencies also included extensions\r\n\r\n## 🔍 Search Terms\r\n\r\n\u003c!--\r\n 💡 Did you know? TypeScript has over 2,000 open suggestions!\r\n 🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.\r\n The \"Common Feature Requests\" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests\r\n\r\n Replace the text below:\r\n-->\r\n\r\nenforce typescript .js extension in import path\r\n\r\n## ✅ Viability Checklist\r\n\r\n\u003c!--\r\n Suggestions that don't meet all these criteria are very, very unlikely to be accepted.\r\n We always recommend reviewing the TypeScript design goals before investing time writing\r\n a proposal for ideas outside the scope of the project.\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\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\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, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).","author":{"url":"https://github.com/alshdavid","@type":"Person","name":"alshdavid"},"datePublished":"2023-01-12T01:10:29Z","url":"https://github.com/microsoft/TypeScript/issues/52204"}
| 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:bd052f77-de33-f45b-3a64-4d6be83813ce |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B7AC:317608:CBE80A:103AFEF:6B00FEB8 |
| html-safe-nonce | 94c93db46ee035f564ec1523039505bbc16ba154e3fbbf4d309d76db56ef1552 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCN0FDOjMxNzYwODpDQkU4MEE6MTAzQUZFRjo2QjAwRkVCOCIsInZpc2l0b3JfaWQiOiIzNTA1MDk5MDgyMzY2NTE2OTIwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5de64f995177340bbbdfedef321f6b6c277755a1a29224aa410a6d4ab20a8894 |
| hovercard-subject-tag | issue:1529937611 |
| 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/52204/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c0642ef1e46ff1df703fc1aea8b3b3641bce8968ca8d5f6fbf16634a6f2f3054/microsoft/TypeScript/issues/52204 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c0642ef1e46ff1df703fc1aea8b3b3641bce8968ca8d5f6fbf16634a6f2f3054/microsoft/TypeScript/issues/52204 |
| og:image:alt | Suggestion Previously, the TypeScript team have made it clear that the compiler will not modify/rewrite import path extensions to convert TypeScript imports to valid ESModule imports - that's total... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alshdavid |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width