问题:
每当我在一个以上的类型脚本文件中导入相同的NPM模块时,只要我运行类型脚本编译器tsc
,我就得到错误"error TS2451: Cannot redeclare block-scoped variable 'os'
".我的项目设置如下所示.
我正在使用NodeJS v18.15.0.
设置:
目录 struct
Project_Root
├─ 双手
├─ Worker.ts
├─ Package.json
└─ Tsconfig.json
双手
const worker = require('./worker');
const os = require('os');
console.log('name = ' + worker.getName() );
console.log('memory = ' + os.freemem() );
Worker.ts
const os = require('os');
module.exports =
class worker {
static getName() : string { return "worker-" + os.freemem(); }
}
Package.json
{
"devDependencies": {
"@types/node": "^18.15.11",
"typescript": "^5.0.3"
}
}
Tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": false
},
"exclude": ["node_modules"]
}
项目要求:
- Requirement #1:没有外部包(RequireJS/Babel/webpack/等),磁盘空间有限.
- Requirement #2:IDE Inteli-Sense应该有效.我们可以使用任何IDE.
- Requirement #3:我们需要使用CommonJS风格的"Required"关键字进行导入.
我try 了以下几点:
我在以下帖子中try 了前三名的答案,但没有成功:
Related Posts | Outcome |
---|---|
Error TS2451: Cannot redeclare block-scoped variable '$' | Same error (after adding the "skipLibCheck: true" flag) |
How to solve this Cannot redeclare block scoped variable 'ngDevMode'? | Not Applicable (I am not using Angular) |
TS2451: Cannot Redeclare Block-scoped Variable | Not Applicable (I am not using ESLint) |
Getting build error in angular CLI: Cannot redeclare block-scoped variable 'ngDevMode' | Not Applicable (I am not using Angular) |
Import npm JS Module in TypeScript | Not Applicable (I am not using Electron) |