【问题标题】:TypeScript error: Cannot write file 'index.d.ts' because it would overwrite input fileTypeScript 错误:无法写入文件“index.d.ts”,因为它会覆盖输入文件
【发布时间】:2016-05-03 20:22:16
【问题描述】:

我在运行tsc时遇到问题

error TS5055: Cannot write file 'index.d.ts' because it would overwrite input file.

我的tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": true,
    "newLine": "LF",
    "preserveConstEnums": true,
    "pretty": true,
    "experimentalDecorators": true
  },

  "exclude": [
    "node_modules",
    "typings"
  ]
}

此问题在以下情况下解决:

  1. tsconfig 中排除index.ts
  2. 运行tsc index.ts
  3. tsconfig 中关闭declaration
  4. index重命名为另一个名字!

在 package.json 中更改 typescript 主文件时我遇到了同样的问题
例如:将 index.ts 重命名为 foo.ts

package.json 更改为

"typescript": {
  "main": "foo.ts"
}

tsc 错误:

error TS5055: Cannot write file 'index.d.ts' because it would overwrite input file.

文件内容无所谓,任何代码内容都有同样的问题!

我能做些什么来修复它?

源码:https://github.com/AliMD/Node.js-Telegram-Bot-API/tree/v0.0.2-0
提前谢谢你。

【问题讨论】:

  • 我在tsconfig临时排除了index.ts来解决这个问题,并在index.ts更改时单独运行tsc index.ts! :(
  • 也许你在代码中的某个地方引用了index.d.ts,然后当你编译时,编译器认为index.d.ts 是源的一部分,因此抛出该错误。只是一个想法。
  • 如果你在没有"declaration": true的情况下编译会发生什么?
  • 什么意思?
  • 您在 cmets 上没有得分,但您是对的,我不知道答案,但我提出问题/提出建议是为了尝试帮助您解决或找到解决您的问题,直到它解决。别客气。并且 outDir 可以与问题相关,因为它将源与输出分开,然后它不应该抱怨在源文件上写入,也许,只是一个想法,但也许它不会工作,该死,不会'没有得到我宝贵的积分。

标签: typescript


【解决方案1】:

也许更好的方法是从您的构建中排除target 目录。关键是在 outDirexclude 键中包含相同的目录:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": true,
        "experimentalDecorators": true,
        "outDir": "target",
        "declaration": true
    },
    "exclude": [
        "node_modules",
        "target"
    ]
}

【讨论】:

    【解决方案2】:

    在您的示例文件夹中,您写了import * as test from '../'
    https://github.com/AliMD/Node.js-Telegram-Bot-API/blob/v0.0.2/examples/test-server.ts#L1

    它应该加载您的index.tsindex.d.tspackage.json“打字”字段。
    这就是问题所在。你的package.json 确实说index.d.ts 是这个包的定义文件,见https://github.com/AliMD/Node.js-Telegram-Bot-API/blob/v0.0.2/package.json#L9
    所以import * as test from '../' 将加载package.json,加载类型字段,然后加载index.d.ts,这会导致问题。

    两种解决方案

    • 导入索引文件而不是根文件夹(推荐方法)
      例如import * as test from '../index';或者,

    • 将输出移动到不同的文件夹
      例如\lib\index.d.ts

    在这两种解决方案中,您都应该加载目标文件而不是文件夹。由于您不再加载根文件夹,因此问题将得到解决。

    【讨论】:

    • 链接损坏,您最好复制使用 URL 的代码 sn-p
    • @Al-Mothafar 谢谢,我更新了链接,正如你所见,我编写了代码并链接了源代码以获取更多信息
    • 值得注意的是,如果您从“./”导入并且恰好位于顶层,这同样适用,这在搜索这种使用模式时很有帮助
    【解决方案3】:

    非常感谢其他所有人的回答,但对我来说,问题要简单得多(也更愚蠢)。我让 WebStorm 为我选择了最好的导入语句,但忽略了它包含“dist”目录中的一个文件(这是我的输出设置。

    一旦找到原因,错误就更有意义了,因为它确实在尝试写入作为输入使用的输出文件。

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题,而且解决起来非常简单。我删除了 node_modules,然后删除了 typings 文件夹。

      在 package.json 我有这些

      scripts: {
          ...
          "tsc": "tsc",
          "typings": "typings"
          ...
      }
      

      然后运行命令:

      npm install
      npm run tsc
      npm run typings install
      

      【讨论】:

        【解决方案5】:

        在我的情况下,我在 tsconfig.json 中缺少 "include": ["./src/**/*.ts"],即使我设置了以下内容:

            "compilerOptions": {
                "outDir": "./build",
                "rootDir": "./src"
            },
        

        Typescript 可能对需要包含哪些文件感到困惑,并且可能认为我在 ./build/index.d.ts 中构建的文件是输入文件。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-10-02
          • 1970-01-01
          • 2017-11-10
          • 1970-01-01
          • 2011-05-08
          • 1970-01-01
          • 1970-01-01
          • 2022-01-17
          相关资源
          最近更新 更多