Typescript错误TS5055将JS文件导入TS

发布于 2025-02-13 11:37:12 字数 880 浏览 0 评论 0原文

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

这是一个tsconfig.json

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

,这是示例ts文件:

import { lib } from "./lib/impotr.js"

console.log(["imported", lib.hello("friend")]);

打字稿编译器给出此错误:

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

如何在tsconfig中修复它?

完整示例img

I want to mix .js and .ts files in the same project, but there is an error (TS5055) that i want to fix, which appears when the project is compiled with tsc, although the output is fine.

here is a tsconfig.json

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

and here is example ts file:

import { lib } from "./lib/impotr.js"

console.log(["imported", lib.hello("friend")]);

typescript compiler gives this error:

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

How to fix it in tsconfig?

full example img

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2025-02-20 11:37:12

看来您没有指定Outdir
看看 this

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

如果此之后仍然存在错误,建议将allowjs设置为false

It seems you didn't specify an outDir.
Take a look at this:

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

If the error is still present after this, it is recommended to set allowJs to false

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文