Title: Ambient Module Declarations for Import Attributes (formerly known as Import Assertions) · Issue #46135 · microsoft/TypeScript · GitHub
Open Graph Title: Ambient Module Declarations for Import Attributes (formerly known as Import Assertions) · Issue #46135 · microsoft/TypeScript
X Title: Ambient Module Declarations for Import Attributes (formerly known as Import Assertions) · Issue #46135 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms import attributes, import assertions, ambient module ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the...
Open Graph Description: Suggestion 🔍 Search Terms import attributes, import assertions, ambient module ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/...
X Description: Suggestion 🔍 Search Terms import attributes, import assertions, ambient module ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScr...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/46135
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Ambient Module Declarations for Import Attributes (formerly known as Import Assertions)","articleBody":"# Suggestion\n\n## 🔍 Search Terms\n\nimport attributes, import assertions, ambient module\n\n## ✅ Viability Checklist\n\nMy suggestion meets these guidelines:\n\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\n* [x] This could be implemented without emitting different JS based on the types of the expressions\n* [x] This isn't a runtime feature (e.g., library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\n\n\n## ⭐ Suggestion\n\n\u003c!-- A summary of what you'd like to see added or changed -->\n\nFollowing the suggestion at https://github.com/microsoft/TypeScript/issues/40694#issuecomment-918343758\nAllow projects to declare ambient modules based on the import attributes. Such as:\n\n```ts\ndeclare module '*' with {type: 'css'} {\n const stylesheet: CSSStyleSheet;\n export default stylesheet;\n}\n```\n\n## 📃 Motivating Example\n\nCurrently frontend build tools use file extensions and URL query strings to support all kinds of custom modules that are eventually compiled into JavaScript.\n\nFor example, in Vite (webpack supports these features in a similar way), we support JS modules importing `.module.css` files as CSS modules, and adding `?url` postfixes to the imported path means we are to import the asset's real URL after bundling.\n\nThat is:\n```js\n// get the exported classes from a CSS module\nimport classes from './example.module.css'\n\n// Explicitly load assets as URL\nimport assetAsURL from './asset.js?url'\n```\n\nBut:\n1. The extension approach may collide with new browser native features, such as [the native CSS modules](https://css-tricks.com/css-modules-the-native-ones/). So it's better to have a way to differentiate them. Import attributes could help in this case.\n2. The query string approach is not a standard.\n - It alters the standard semantics of an import statement\n - Different tools vary on their choices of queries\n - Modules with such syntaxes won't be able to run separately without *the specific bundler*\n - So, we hope to migrate away from this approach and base the feature upon a standard. Import attributes seems to be the way to go.\n\nOne thing that blocks us from doing so is the ability to declare ambient modules based on import assertions in TypeScript.\n\nWe can easily have type definitions for files based on their file extensions and query strings. In vite we have: https://github.com/vitejs/vite/blob/b690810f555549e9eed4b03600b27fe7649d6b07/packages/vite/client.d.ts\n\nBut currently, I don't see a way to migrate these type definitions when we move to import assertions.\n\n## 💻 Use Cases\n\n\u003c!--\n What do you want to use this for?\n What shortcomings exist with current approaches?\n What workarounds are you using in the meantime?\n-->\n\n1. The abovementioned frontend build tools.\n2. Type definitions for browsers' built-in CSS & JSON modules support. We can have `declare module '*' assert {type: 'json'}` and `declare module '*' assert {type: 'css'}` in the DOM lib.","author":{"url":"https://github.com/haoqunjiang","@type":"Person","name":"haoqunjiang"},"datePublished":"2021-09-29T14:01:07Z","url":"https://github.com/microsoft/TypeScript/issues/46135"}
| 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:5ffb7c93-5e70-3128-dc43-a0642b6ba014 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD46:3897F3:B97B18:F0CD71:6B00ED5C |
| html-safe-nonce | 0c644b622f93b29b01a1f3a0bf7d440d20be945b1d744fce6bf2655b021196fd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDQ2OjM4OTdGMzpCOTdCMTg6RjBDRDcxOjZCMDBFRDVDIiwidmlzaXRvcl9pZCI6IjQyNDIxMjU1ODkzMDMwNjE4NTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4d0628d6f531bea27c8ecf460a7a72cd772ee1bb8fe0974ebf00f044330623d7 |
| hovercard-subject-tag | issue:1011000366 |
| 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/46135/issue_layout |
| twitter:image | https://opengraph.githubassets.com/affd4ec8eef70b2b186dbc4c04644bcebbf79e1fb31e956d469e16f27faa44d1/microsoft/TypeScript/issues/46135 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/affd4ec8eef70b2b186dbc4c04644bcebbf79e1fb31e956d469e16f27faa44d1/microsoft/TypeScript/issues/46135 |
| og:image:alt | Suggestion 🔍 Search Terms import attributes, import assertions, ambient module ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | haoqunjiang |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width