我正在为几个项目开发一个typescript库。它包含一些包,每个包都有一些模块,但tsc为我生成的声明文件有问题。
我创建了一个可能更容易查看的回购。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",]
}
现在,在模块A.ts中,我有一些类似的代码,例如
export class Foo {
foo(): string {
return "foo";
}
}
在软件包A/index.ts中,我有:
export { Foo } from "./moduleA.ts"
包B/模块B.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";
}
}
这一切的问题在于它是有效的。导入看起来不错,对我来说真的很容易。但当我构建并发布它时,我在typescript声明文件中得到了以下内容。
模块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