Use CommonJS and AMD code in Typescript

| Tag typescript  modules 

When consuming a JS module that used the CommonJS or AMD standard, we can simply import names from it, just like for ES2015 modules:

import {something} from './a/legacy/commonjs/module';

use default export

By default, CommonJS default exports don’t interoperate with ES2015 default imports; to use a default export, we have to use a wild import:

import * as fs from 'fs';
fs.readFile('some/file.txt');

To interoperate more smoothly, set {"esModuleInterop": true} in tsconfig.json’s compilerOptions. Now we can leave out the wildcard:

import fs from 'fs';
fs.readFile('some/file.txt');

Prev     Next