库的打字稿声明文件找不到模块

大坏蛋

我正在为我正在从事的一些项目开发打字稿库。它包含一些包,每个包都有一些模块,但是我遇到了 tsc 为我生成的声明文件的问题。

我创建了一个 repo 可能更容易查看。https://github.com/BenMcLean981/typescript-library-issue

我的代码或多或少的结构如下:

.
├── src
│   ├── packageA
|   |   ├──moduleA.ts
│   │   └──index.ts
│   └── packageB
|       ├──moduleB.ts
│       └──index.ts
└── tsconfig.json

我的 tsconfig 如下:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "lib": ["ES2019"],
    "sourceMap": true,
    "baseUrl": "./src",
    "esModuleInterop": true,
    "moduleResolution": "node"
  },
  "include": ["src"],
  "exclude": ["node_modules",]
}

现在,在 moduleA.ts 我有一些这样的代码

export class Foo {
  foo(): string {
    return "foo";
  }
}

在 packageA/index.ts 我有:

export { Foo } from "./moduleA.ts"

包B/moduleB.ts:

import { Foo} from "moduleB" //Note here that I import directly form the package.
// This is how I want my library to be consumed.

export class Bar extends Foo {
  bar(): string {
    return "bar";
  }
}

所有这一切的事情是它的工作原理。进口看起来不错,对我来说真的很容易处理。但是当我去构建它并发布它时,我在我的打字稿声明文件中得到以下内容。

模块B.d.ts

import { Foo } from "packageA"; //Cannot find module 'packageA' or its corresponding type declarations.ts(2307)
export declare class Bar extends Foo {
    bar(): string;
}

我很确定这是我的 tsconfig 的问题。我不明白所有的设置。任何帮助将非常感激!

大坏蛋

正如Amir Saleem建议的那样,将以下内容添加到我的 tsconfig 解决了我的问题:

"declarationMap": true

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章