typescript - 为什么在 Node.js 代码中同时使用 import 和 require()?
我正在阅读 “Express 4.16 的类型定义” 的源代码,发现了这一行有趣的代码(#18):
import serveStatic = require("serve-static");
由于 import 是在 ES6 中使用模块的新方式,为什么上面的代码仍然被使用或需要呢?
答案#1
Express 4.16 的类型定义(index.d.ts)是用 TypeScript 编写的,其中 import=require() 是 TypeScript 语法
TypeScript - 模块(export=和 import=require())
export = 语法指定从模块导出的单个对象。这个对象可以是类、接口、命名空间、函数或枚举。
当使用 export = 导出模块时,必须使用 TypeScript 特有的 import module=require("module") 来导入模块。
参考:模块