在ts文件中总是提示这种错误
1, 原因:
之所以 tslint 会提示这个错误,是因为在 Commonjs 规范里,没有像 ESModule 能形成闭包的「模块」概念,所有的模块在引用时都默认被抛至全局,因此当再次声明某个模块时,TypeScript 会认为重复声明了两次相同的变量进而抛错。
2, 解决方案
在单ts文件末尾,添加
export {}
在tsconfig.json文件中esModuleInterop 配置为true
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"esModuleInterop": true, // 允许文件中出现 export 关键字。
"target": "esnext",
"strict": true,
"outDir": "app",
"declaration": true,
"sourceMap": true
}
3, 在项目中,建议通过下面这种方案解决
1, 安装 @babel/plugin-transform-modules-commonjs插件
npm install --save-dev @babel/plugin-transform-modules-commonjs