Typescript如何导入CommonJs?

1,399 阅读1分钟

使用import a from 'a'试图导入一个CommonJS模块a会发生错误, 因为CommonJSdefault导出

解决方法

  1. 使用import a = require('a')语法
  2. 使用import * as a from 'a'语法
  3. recommend: 使用--esModuleInterop--allowSyntheticDefaultImports

参考

Importing commonJS modules with es6 imports

Synthesize namespace records on CommonJS imports if necessary

understanding-esmoduleinterop-in-tsconfig-file