ES import

import x from ‘x’

image.png
在 es 中,一般希望采取直接 import x 的方式来进行导入,但是直接 import x 的语义是模块导出的 default,也就是 module.exports.default,而一般的 commonjs 库是不会刻意采用 default 做具名导出的。
更常见的做法是 module.exports = x 作为整库导出,所以当然没有 default,ts 开启该选项,其实就是将 commonjs 的 module.exports = x,当作 export default x 来使用。或者是将module.exports上加上default也就是 module.exports.default = x。
image.png
从这个函数也可以看到,将普通的 commonjs导出 转化为 es导出,只需要做到:

  1. 标注公认标识 __esModule: true;
  2. 具名default导出;

即可正常使用 import x from ‘x’;
当然,如果没有开启该配置,且非es导出,使用 import * as x from ‘x’ 也没有什么问题。此时的 x = module.exports。