Title: [Design Spec] ESNext import() · Issue #14495 · microsoft/TypeScript · GitHub
Open Graph Title: [Design Spec] ESNext import() · Issue #14495 · microsoft/TypeScript
X Title: [Design Spec] ESNext import() · Issue #14495 · microsoft/TypeScript
Description: import(specifier) is a newly proposed syntax for dynamically module loading. import(specifier) takes an assignment expression as an input (Note: unlike ES6 static import which only allow string literal as module specifier) and returns a ...
Open Graph Description: import(specifier) is a newly proposed syntax for dynamically module loading. import(specifier) takes an assignment expression as an input (Note: unlike ES6 static import which only allow string lit...
X Description: import(specifier) is a newly proposed syntax for dynamically module loading. import(specifier) takes an assignment expression as an input (Note: unlike ES6 static import which only allow string lit...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/14495
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Design Spec] ESNext import()","articleBody":"`import(specifier)` is a newly proposed syntax for dynamically module loading. `import(specifier)` takes an assignment expression as an input (Note: unlike ES6 static import which only allow string literal as [module specifier](https://tc39.github.io/ecma262/#prod-ModuleSpecifier)) and returns a promise of [module namespace object](https://tc39.github.io/ecma262/#sec-module-namespace-objects).\r\n\r\nThe full proposal can be found [here](https://github.com/tc39/proposal-dynamic-import)\r\nOriginal issue: #12364\r\n\r\nThis issue will discuss design proposal for implementing `import()`: parsing, type-checking, and emitting. \r\n\r\nThe proposal will NOT include \"module namespace object\" type and expressing the shape of the loaded module (To be able to express such type will allow users to cast the result instead of \"any\" to be what they expected at runtime)...The issue will be discussed and implemented separately.\r\n\r\n**Parsing**\r\n`import(specifier)` will be parsed as [CallExpression](https://tc39.github.io/proposal-dynamic-import/#prod-ImportCall). and `specifier` will be parsed as [AssignmentExpression](https://tc39.github.io/ecma262/#prod-AssignmentExpression)\r\n\r\n**Error**\r\n\r\n- Error if the `module` flag is ES2015.\r\n\r\n- Error if `specifier` is not assignable to string type\r\n\r\n**Type-checking**\r\n\r\n- `specifier` is type checked as string type. (Clarification: the only limitation for `specifier` in original proposal is that it can called `toString()`)\r\n- The return of `import(specifier)` will be as following\r\n-- if known module can be resolved from `specifier`, then we return Promise of module namespace object \r\n-- if not known module, then we return Promise of any. ~~This will give an error in `noImplicitAny` mode and `import()` is used as an expression~~ After offline discussion with @mhegazy @bowdenk7 @DanielRosenwasser, we think reporting an error without providing a way to easily get out of it (see #14844 for more detail) is undesirable. By casting to `Promise\u003cany>` to satisfy the compiler will not provide users any benefit and arguably is a drawback because if in the future, there is a way to type the module easily, upgrading to use such syntax will not be convenient. Therefore, we will not issue an `noImplicitAny` error with this feature but with #14844 instead.\r\n\r\n- We will use existed module resolution logic to try to figure out the module of the dynamic import.\r\n\r\n**Emit**\r\nEmit for each different `module` kind emit is outlined below\r\n```typescript\r\nimport('blah');\r\n```\r\n- Node\r\n```js\r\nPromise.resolve().then(() => require('blah'));\r\n```\r\n\r\n- AMD\r\n```js\r\ndefine([\"require\", \"exports\"], function (require, exports) {\r\n \"use strict\";\r\n Object.defineProperty(exports, \"__esModule\", { value: true });\r\n new Promise(resolve => require(['blah'], resolve));\r\n});\r\n```\r\n- UMD\r\n\r\n``` js\r\n(function (factory) {\r\n if (typeof module === \"object\" && typeof module.exports === \"object\") {\r\n var v = factory(require, exports);\r\n if (v !== undefined) module.exports = v;\r\n }\r\n else if (typeof define === \"function\" && define.amd) {\r\n define([\"require\", \"exports\"], factory);\r\n }\r\n})(function (require, exports) {\r\n \"use strict\";\r\n Object.defineProperty(exports, \"__esModule\", { value: true });\r\n require.length === 1 ?\r\n /*CommonJs Require*/ Promise.resolve().then(() => require('blah'));\r\n /*Amd Require*/ new Promise(resolve => require(['blah'], resolve));\r\n});\r\n```\r\n\r\n- System (Updated from [comment](https://github.com/Microsoft/TypeScript/issues/14495#issuecomment-285294096))\r\n```js\r\nSystem.register([], function (_export, _context) {\r\n return {\r\n setters: [],\r\n execute: () => {\r\n _context.import('dynamic');\r\n }\r\n };\r\n});\r\n```\r\n\r\n- ES2018 _new value_\r\n```js\r\nimport('blah')\r\n```\r\n","author":{"url":"https://github.com/yuit","@type":"Person","name":"yuit"},"datePublished":"2017-03-06T23:49:33Z","url":"https://github.com/microsoft/TypeScript/issues/14495"}
| 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:5bf33282-9f82-4c05-4f10-d91028c51e29 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8DD6:311EEE:DCF043:126883B:6B00FEE2 |
| html-safe-nonce | 12cd9e5b7c8247d332b6b19c55807704260abf5d8c0acf64009143bf07574b9f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REQ2OjMxMUVFRTpEQ0YwNDM6MTI2ODgzQjo2QjAwRkVFMiIsInZpc2l0b3JfaWQiOiIyMjgzNjMzODAzODQwNzgyMDUxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 91aa9e4137360c702f9071bfedbc6f44119a3850578485ad5b36a4d19abcb006 |
| hovercard-subject-tag | issue:212287038 |
| 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/14495/issue_layout |
| twitter:image | https://opengraph.githubassets.com/392ac1027c6cad042dc5017d9948643f6cfbe76b0e71ba67bb866a87c2c23d06/microsoft/TypeScript/issues/14495 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/392ac1027c6cad042dc5017d9948643f6cfbe76b0e71ba67bb866a87c2c23d06/microsoft/TypeScript/issues/14495 |
| og:image:alt | import(specifier) is a newly proposed syntax for dynamically module loading. import(specifier) takes an assignment expression as an input (Note: unlike ES6 static import which only allow string lit... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yuit |
| 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