TypeScript `.d.ts` 文件未复制到输出,导致引用损坏

TypeScript `.d.ts` file not copied to output, causing broken reference

在我的 Typescript 文件中,我像这样从声明文件中导入一个类型。

在example.ts中:

import { AnyObject } from './index;
export const obj: AnyObject = {};

然后当我 运行 像这样编译命令时。 tsc -d

它将生成 example.js 和 example.d.ts 但不会生成 index.d.ts。

在example.d.ts:

import { AnyObject } from './index';
export declare const obj: AnyObject;

显然找不到index.d.tsmodule.So我想知道如何解决这个问题,谢谢。

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": false,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "lib": [
        "es2017",
        "dom"
    ],
    "jsx": "react"
  },
  "include": [
    "./packages/*"
  ]
}

如果我没理解错的话,你的源目录有 example.tsindex.d.ts,你的输出目录有 example.jsexample.d.ts 但没有 index ,因此 example.d.ts 中的引用已损坏。基于 this issue (found in a web search),该行为是故意的,我认为除了将 index.d.ts 重命名为 index.ts 或使用单独的工具将其复制到输出目录之外,没有其他解决方案。