Typescript错误TS5055导入js文件到ts.



我想在同一个项目中混合.js和.ts文件,但是有一个错误(TS5055)我想修复,当项目用tsc编译时出现,虽然输出很好。

这里是一个tsconfig.json

{
"compilerOptions": {
"target": "es2022",
"module": "ES2022",
"allowJs": true,
"lib": [
"es2022",
"DOM"
],
"removeComments": true
},
"files": [
"./index.ts"
]
}

,下面是示例文件:

import { lib } from "./lib/impotr.js"
console.log(["imported", lib.hello("friend")]);

typescript编译器给出这个错误:

PS D:WHY> tsc -p .
error TS5055: Cannot write file 'D:/WHY/lib/impotr.js' 
because it would overwrite input file.

如何在tsconfig中修复它?

完整的示例img

您似乎没有指定outDir。看看这个:

{
"compilerOptions": {
"target": "es2022",
"module": "ES2022",
"allowJs": true,
"outDir": "build",
"lib": [
"es2022",
"DOM"
],
"removeComments": true
},
"files": [
"./index.ts"
]
}

如果错误仍然存在,建议将allowJs设置为false