ecmascript 6 - TypeScript Importing from libraries written in ES5 vs. ES6 -
i weird errors when running transpiled typescript code depends on external libraries
such uncaught typeerror: es5_lib_1.default not function
what's wrong?
the es6 module spec differ commonjs, described here. introduces compatibility issues exasperated in typescript.
typescript tries guess correct way transpile import/require statements based on 2 inputs
- the
moduleproperty intsconfig.json - how
exportstatement written in corresponding.d.tsfile
in tsconfig.json file, can set module format transpiled output use. example, module: 'es6'
what choose here have impact on kind of importing syntax typescript allow. impacted how corresponding .d.ts shape files written.
if importing from commonjs library , our output module targets commonjs, must use
//tsconfig.json module: "commonjs" //.d.ts declare module 'foo' { exports = foo; } // app.ts import foo = require('foo'); if importing from commonjs library , our output module targets es6, must use:
//tsconfig.json module: "es6" //.d.ts declare module 'foo' { exports default foo; } // app.ts import {default foo} 'foo'; if importing from es6 library, can use import {default ... } style regardless of targeted output module format
//tsconfig.json module: "es6|commonjs" //.d.ts declare module 'foo.es6' { exports default fooes6; } // app.ts import {default fooes6} 'foo.es6'; what mean .d.ts files retrieve tsd install?
depending on output targeting, may have alter .d.ts files after they've been installed suit our needs. .d.ts files written commonjs modules , use export = <lib> style. if want target es6 output, need edit , change export default
Comments
Post a Comment