Previously I could do:
import foo = require('foo');
But now that TypeScript (1.5) supports es6 module syntax, what is the correct way to achieve the same in ES6 module syntax.
Previously I could do:
import foo = require('foo');
But now that TypeScript (1.5) supports es6 module syntax, what is the correct way to achieve the same in ES6 module syntax.
Another option is to import it using commonjs syntax:
TypeScript and Bable both agree on what to do with this. Also, if you're compiling down to ES5 or less anyway, then this won't be too far off from it's final form.
The corresponding syntax for ES6 module syntax is:
Basically import everything from the
foo
module into a local variable by the name offoo
.The correct way is to continue using the old import syntax. The new import syntax is for ES modules only, the old import syntax is for pre-ES6 modules. The two are distinct, intentionally so.
import * as foo from 'foo'
imports all the properties of the module 'foo', it does not import the default value asfoo
.From the designer of the feature:
export default
consistently has ES module semantics. For compatibility with Babel we could optionally emit an__esModule
marker when a module has a default export, but we wouldn't actually use that marker for anything.export =
declaration, which substitutes a different entity to be exported in place of the module itself, is always emitted as an assignment tomodule.exports
. It is an error to have other exports in a module that usesexport =
. This is the existing TypeScript behavior.export =
to export another module (be that an internal or external module) can be imported using the new ES6 constructs. In particular, the convenient destructuring imports can be used with such modules. The pattern of usingexport =
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).export =
to export a non-module entity in place of the module itself must be imported using the existingimport x = require("foo")
syntax as is the case today.2016 update: The TypeScript compiler at some point started allowing
import * as foo from 'legacy-module-foo'
to get the default import of a legacy module in certain circumstances. This is a violation of the ES6 specification (§15.2.1.16, “The value "*" indicates that the import request is for the target module’s namespace object.”).When legacy modules you import in this manner are updated to ES6 modules, the “default” imports for those modules will stop working (because
* as foo
imports are supposed to be importing namespace objects), which may be extremely confusing if you don’t know that doing this is a TypeScript/SystemJS hack. It is also possible that a future TypeScript realignment to the ES specification will cause them to break.As such, you should probably prefer to continue to use the legacy import syntax described above to load legacy modules to avoid confusing yourself and other developers working on your code about how ES6 namespace imports work, and to avoid confusing breaking changes.
As of TypeScript 2.7, there is a new
esModuleInterop
flag that can be used to enable default imports with CommonJS/AMD/UMD. By setting that flag totrue
in yourtsconfig.json
, this should work as expected:Source
As far as I understand, that means that you are encouraged to migrate your own TypeScript modules to the new syntax, but keep using
import foo = require('foo')
for importing actual AMD/CommonJS modules.