Title: ES6 Modules · Issue #2242 · microsoft/TypeScript · GitHub
Open Graph Title: ES6 Modules · Issue #2242 · microsoft/TypeScript
X Title: ES6 Modules · Issue #2242 · microsoft/TypeScript
Description: This issue describes TypeScript's support for ECMAScript 6 modules as implemented in #1983, #2197, and #2460. TypeScript 1.5 supports ECMAScript 6 (ES6) modules. ES6 modules are effectively TypeScript external modules with a new syntax: ...
Open Graph Description: This issue describes TypeScript's support for ECMAScript 6 modules as implemented in #1983, #2197, and #2460. TypeScript 1.5 supports ECMAScript 6 (ES6) modules. ES6 modules are effectively TypeScr...
X Description: This issue describes TypeScript's support for ECMAScript 6 modules as implemented in #1983, #2197, and #2460. TypeScript 1.5 supports ECMAScript 6 (ES6) modules. ES6 modules are effectively Typ...
Mail addresses
notifications@github.com
Opengraph URL: https://github.com/microsoft/TypeScript/issues/2242
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ES6 Modules","articleBody":"This issue describes TypeScript's support for [ECMAScript 6 modules](http://www.2ality.com/2014/09/es6-modules-final.html) as implemented in #1983, #2197, and #2460.\n\nTypeScript 1.5 supports ECMAScript 6 (ES6) modules. ES6 modules are effectively TypeScript external modules with a new syntax: ES6 modules are separately loaded source files that possibly import other modules and provide a number of externally accessible exports. ES6 modules feature several new export and import declarations. It is recommended that TypeScript libraries and applications be updated to use the new syntax, but this is not a requirement. The new ES6 module syntax coexists with TypeScript's original internal and external module constructs and the constructs can be mixed and matched at will.\n\nIn TypeScript 1.5, a source file is considered an external module if it contains at least one of the following:\n- A top-level declaration that specifies an `export` modifier.\n- An new ES6 export or import declaration of any form.\n- An original TypeScript export-equals assignment of the form `export = Point`.\n- An original TypeScript import-equals statement of the form `import Math = require(\"math\")`.\n\nAn external module has a set of _exports_ that are specified using various forms of export declarations. Those exports can be imported into local name bindings in other modules using various forms of import declarations.\n\nAn external module may designate a _default export_, which is an export with the reserved name `default`. A number of short-hand export and import declaration constructs exist to facilitate easy export and import of the default entity.\n\nFor backwards compatibility with CommonJS and AMD style modules, TypeScript also supports export-equals declarations of the form `export = Point`. Unlike default export declarations, which are just shorthand for an export named `default`, export-equals declarations designate an entity to be exported _in place of_ the actual module.\n\nAs ES6 modules gain adoption, TypeScript's original export-equals and import-equals declarations are expected to become legacy.\n## Export Declarations\n\nWhen a declaration specifies an `export` modifier, each declared name is exported from the containing module exactly as is the case with original TypeScript external modules. For example:\n\n``` typescript\nexport interface Stream { ... }\nexport function write(stream: Stream, data: string) { ... }\n```\n\nModule members can also be exported using separate export declarations, and such declarations can specify different names for exports using `as` clauses. For example:\n\n``` typescript\ninterface Stream { ... }\nfunction writeToStream(stream: Stream, data: string) { ... }\nexport { Stream, writeToStream as write }; // writeToStream exported as write\n```\n\nAn export declaration exports all meanings of a name. For example:\n\n``` typescript\ninterface Stream { ... }\nfunction Stream(url: string): Stream { ... }\nexport { Stream }; // Exports both interface and function\n```\n### Re-exporting\n\nAn export declaration that specifies a `from` clause is a re-export. A re-export copies the exports of a given module to the current module without introducing local names.\n\n``` typescript\nexport { read, write, standardOutput as stdout } from \"./inout\";\n```\n\nAn `export *` declaration can be used to re-export all exports of another module. This is useful for creating modules that aggregate the exports of several other modules.\n\n``` typescript\nexport function transform(s: string): string { ... }\nexport * from \"./mod1\";\nexport * from \"./mod2\";\n```\n\nAn `export *` doesn't re-export default exports or exports with names that are already exported from the current module. For example, the `transform` export in the module above hides any `transform` export in the re-exported modules.\n### Default Export\n\nAn export default declaration specifies an expression that becomes the default export of a module:\n\n``` typescript\nexport default {\n name: \"hello\",\n count: 42\n};\n```\n\nAn export default declaration is just a short-hand way of exporting an entity with the name `default`. For example, the module above could instead be written:\n\n``` typescript\nconst x = {\n name: \"hello\",\n count: 42\n};\nexport { x as default };\n```\n\nWhen an export default specifies a single identifier, all meanings of that identifier are exported:\n\n``` typescript\ninterface Stream { ... }\nfunction Stream(url: string): Stream { ... }\nexport default Stream; // Exports a type and a value\n```\n\nAn export default declaration can directly declare and export a function or class. The function or class can optionally be named so it can be referenced in the implementing module, but the exported name is always `default`.\n\nThe following exports an unnamed function with the exported name `default`:\n\n``` typescript\nexport default function (x: number) {\n return x * x;\n}\n```\n\nThe following exports a class with the local name `Greeter` and the exported name `default`:\n\n``` typescript\nexport default class Greeter {\n sayHello() {\n console.log(\"Greetings!\");\n }\n}\n```\n## Import Declarations\n\nThe exports of a module are imported using import declarations. Import declarations can optionally use `as` clauses to specify different local names for the imports. For example:\n\n``` typescript\nimport { read, write, standardOutput as stdout } from \"./inout\";\nvar s = read(stdout);\nwrite(stdout, s);\n```\n\nAs an alternative to individual imports, a namespace import can be used to import an entire module:\n\n``` typescript\nimport * as io from \"./inout\";\nvar s = io.read(io.standardOutput);\nio.write(io.standardOutput, s);\n```\n### Default Import\n\nThe default export of a module is particularly easy to import:\n\n``` typescript\nimport Greeter from \"./greeter\";\nvar g = new Greeter();\ng.sayHello();\n```\n\nThe above is exactly equivalent to importing the export named `default`:\n\n``` typescript\nimport { default as Greeter } from \"./greeter\";\nvar g = new Greeter();\ng.sayHello();\n```\n\nIt is possible to import both the default export and named exports in a single import declaration:\n\n``` typescript\nimport defaultExport, { namedExport1, namedExport2, namedExport3 } from \"./myModule\";\n```\n### Bare Import\n\nA \"bare import\" can be used to import a module only for its side-effects. Such an import creates no local name bindings.\n\n``` typescript\nimport \"./polyfills\";\n```\n## CommonJS and AMD Code Generation\n\nTypeScript supports down-level compilation of external modules using the new ES6 syntax.\n- When compiling with `-t ES3` or `-t ES5` a module format must be chosen using `-m CommonJS` or `-m AMD`.\n- When compiling with `-t ES6` the module format is implicitly assumed to be ECMAScript 6 and the compiler simply emits the original code with type annotations removed.\n\nWhen compiling down-level for CommonJS or AMD, named exports are emitted as properties on the loader supplied `exports` instance. This includes default exports which are emitted as assignments to `exports.default`.\n\nBelow are some examples of external modules and the code emitted for CommonJS and AMD.\n\nA module with named exports:\n\n``` typescript\n// TypeScript code\nfunction foo() { }\nfunction bar() { }\nexport { foo, bar as baz };\n\n// Code emitted for CommonJS\nfunction foo() { }\nexports.foo = foo;\nfunction bar() { }\nexports.baz = bar;\n\n// Code emitted for AMD\ndefine([\"require\", \"exports\"], function (require, exports) {\n function foo() { }\n exports.foo = foo;\n function bar() { }\n exports.baz = bar;\n});\n```\n\nA module with a default export:\n\n``` typescript\n// TypeScript code\nexport default function foo() { }\n\n// Code emitted for CommonJS\nfunction foo() { }\nexports.default = foo;\n\n// Code emitted for AMD\ndefine([\"require\", \"exports\"], function (require, exports) {\n function foo() { }\n exports.default = foo;\n});\n```\n\nA module with re-exports:\n\n``` typescript\n// TypeScript code\nexport { read, write } from \"./inout\";\nexport * from \"./utils\";\n\n// Code emitted for CommonJS\nfunction __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n}\nvar inout_1 = require(\"./inout\");\nexports.read = inout_1.read;\nexports.write = inout_1.write;\n__export(require(\"./utils\"));\n\n// Code emitted for AMD\ndefine([\"require\", \"exports\", \"./inout\", \"./utils\"], function (require, exports, inout_1, utils_1) {\n function __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n }\n exports.read = inout_1.read;\n exports.write = inout_1.write;\n __export(utils_1);\n});\n```\n\nImporting a module:\n\n``` typescript\n// TypeScript code\nimport { read, write, standardOutput as stdout } from \"./inout\";\nvar s = read(stdout);\nwrite(stdout, s);\n\n// Code emitted for CommonJS\nvar inout_1 = require(\"./inout\");\nvar s = inout_1.read(inout_1.standardOutput);\ninout_1.write(inout_1.standardOutput, s);\n\n// Code emitted for AMD\ndefine([\"require\", \"exports\", \"./inout\"], function (require, exports, inout_1) {\n var s = inout_1.read(inout_1.standardOutput);\n inout_1.write(inout_1.standardOutput, s);\n});\n```\n\nNote that destructuring import declarations are rewritten to property accesses on the imported module object. This ensures that exported members can circularly reference each other. For example:\n\n``` typescript\n// ------ ping.ts ------\nimport { pong } from \"./pong\";\nexport function ping(count: number) {\n if (count > 0) {\n console.log(\"ping\");\n pong(count - 1);\n }\n}\n\n// ------ pong.ts ------\nimport { ping } from \"./ping\";\nexport function pong(count: number) {\n if (count > 0) {\n console.log(\"pong\");\n ping(count - 1);\n }\n}\n\n// ------ main.ts ------\nimport { ping } from \"./ping\";\nping(10);\n```\n\nThis generates the following code when compiled for CommonJS:\n\n``` javascript\n// ------ ping.js ------\nvar pong_1 = require(\"./pong\");\nfunction ping(count) {\n if (count > 0) {\n console.log(\"ping\");\n pong_1.pong(count - 1);\n }\n}\nexports.ping = ping;\n\n// ------ pong.js ------\nvar ping_1 = require(\"./ping\");\nfunction pong(count) {\n if (count > 0) {\n console.log(\"pong\");\n ping_1.ping(count - 1);\n }\n}\nexports.pong = pong;\n\n// ------ main.js ------\nvar ping_1 = require(\"./ping\");\nping_1.ping(10);\n```\n## Interoperabitility\n\nAn existing external module that doesn't use `export =` is already ES6 compliant and can be imported using the new ES6 constructs with no additional work.\n\nAn external module that uses `export =` to export another module or a \"module like\" entity can also be imported using the new ES6 constructs. In particular, the convenient destructuring imports can be used with such modules. The pattern of using `export =` to export another module is common in .d.ts files that provide a CommonJS/AMD view of an internal module (e.g. [angular.d.ts](https://github.com/borisyankov/DefinitelyTyped/blob/master/angularjs/angular.d.ts)).\n\nA module that uses `export =` to export a non-module entity in place of the module itself must be imported using the existing `import x = require(\"foo\")` syntax as is the case today.\n","author":{"url":"https://github.com/ahejlsberg","@type":"Person","name":"ahejlsberg"},"datePublished":"2015-03-07T00:33:25Z","url":"https://github.com/microsoft/TypeScript/issues/2242"}
| 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:5b28f0ad-f810-04d7-2df7-3bf02272d33f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D506:2D40DE:5150BD:66C32D:6B010E77 |
| html-safe-nonce | 90da460a48387f8dcdc2384dc790b23bf48696a0c00eb397d93bd447b35bb39d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTA2OjJENDBERTo1MTUwQkQ6NjZDMzJEOjZCMDEwRTc3IiwidmlzaXRvcl9pZCI6IjM2MTAwODIzNDIzNzIxODM2NzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d079c66558deaec27e7ebfbedc1bbefaa252577dfb6da70d5ba974a3f86e7eff |
| hovercard-subject-tag | issue:60179656 |
| 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/2242/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3a0a603fea27c8fd851591d41985596550fb48a8b6f268a754c6416b41ad41e9/microsoft/TypeScript/issues/2242 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3a0a603fea27c8fd851591d41985596550fb48a8b6f268a754c6416b41ad41e9/microsoft/TypeScript/issues/2242 |
| og:image:alt | This issue describes TypeScript's support for ECMAScript 6 modules as implemented in #1983, #2197, and #2460. TypeScript 1.5 supports ECMAScript 6 (ES6) modules. ES6 modules are effectively TypeScr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ahejlsberg |
| 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