I am currently developing a TypeScript library for several projects I am involved in. The library contains various packages, each of which consists of multiple modules. However, I am encountering an issue with the declaration file generated by tsc.
To make it easier to understand my problem, I have created a public repository that you can access here.
The structure of my code is organized as follows:
.
├── src
│ ├── packageA
| | ├──moduleA.ts
│ │ └──index.ts
│ └── packageB
| ├──moduleB.ts
│ └──index.ts
└── tsconfig.json
Here is the content of my tsconfig file:
{
"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",]
}
Within moduleA.ts, I have the following code snippet:
export class Foo {
foo(): string {
return "foo";
}
}
In packageA/index.ts:
export { Foo } from "./moduleA.ts"
And in packageB/moduleB.ts:
import { Foo} from "moduleB" //Note here that I import directly form the package.
export class Bar extends Foo {
bar(): string {
return "bar";
}
}
Although everything seems to work fine during development, when I attempt to build and publish the library, I encounter an issue with the resulting TypeScript declaration file:
moduleB.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;
}
I suspect this problem stems from my tsconfig settings, which I admit I do not fully comprehend. Any assistance in resolving this issue would be greatly appreciated!