TypeScript错误:无法写入文件'index.d.ts',因为它将覆盖输入文件

Ali*_*.MD 6 typescript

我跑步时有问题 tsc

error TS5055: Cannot write file 'index.d.ts' because it would overwrite input file.
Run Code Online (Sandbox Code Playgroud)

我的tsconfig.json

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

  "exclude": [
    "node_modules",
    "typings"
  ]
}
Run Code Online (Sandbox Code Playgroud)

该问题在以下情况下得以解决:

  1. 排除index.tstsconfig
  2. tsc index.ts
  3. 关闭declaration在关闭tsconfig
  4. 重命名index为另一个名称!


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

更改package.json

"typescript": {
  "main": "foo.ts"
}
Run Code Online (Sandbox Code Playgroud)

tsc错误:

error TS5055: Cannot write file 'index.d.ts' because it would overwrite input file.
Run Code Online (Sandbox Code Playgroud)

文件内容没有问题,任何代码内容都有相同的问题!

我该如何解决?

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

Bur*_*ris 8

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

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": true,
        "experimentalDecorators": true,
        "outDir": "target",
        "declaration": true
    },
    "exclude": [
        "node_modules",
        "target"
    ]
}
Run Code Online (Sandbox Code Playgroud)


Ali*_*.MD 5

在您编写的示例文件夹中,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

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


kem*_*ica 5

就我而言,"include": ["./src/**/*.ts"]即使我设置了以下内容,我也缺少 tsconfig.json:

    "compilerOptions": {
        "outDir": "./build",
        "rootDir": "./src"
    },
Run Code Online (Sandbox Code Playgroud)

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