简体   繁体   中英

What does assigning a value to 'export' mean in TypeScript?

declare namespace moment {
...
}

export = moment;

This is cited from the TypeScript declaration file of the Moment.js library. What does the last line mean? Is the 'export' keyword here the same as the one in Node.js?

This is TypeScript's 'export =' syntax . From the documentation:

The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

This is not standard ES6 JavaScript, and will likely raise a syntax error if used there. The way that TypeScript transpiles your export depends on the mode it is in:

Depending on the module target specified during compilation, the compiler will generate appropriate code for Node.js (CommonJS), require.js (AMD), isomorphic (UMD), SystemJS, or ECMAScript 2015 native modules (ES6) module-loading systems. For more information on what the define, require and register calls in the generated code do, consult the documentation for each module loader.

I think export = "" a legacy feature for AMD modules, where you typically would write.

define(["depA"], function(depA){
    var someObj = {};     
    return someObj;
});

The only reason I would use it, is to consume the compiled typescript module from a regular javascript/amd scenario. Otherwise, try to avoid it and use the default "export identifier" syntax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM