Search code examples
typescripttsc

Typescript declaration file for library cannot find module


Im working on a typescript library for a few projects Im working on. It holds some packages, and each of those packages have some modules, but I'm having a problem with the declaration file tsc is producing for me.

I created a repo which might be easier to look at. https://github.com/BenMcLean981/typescript-library-issue

My code is more or less structured as follows:

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

My tsconfig is as follows:

{
  "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",]
}

Now, in moduleA.ts I have some code like this for example

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

In packageA/index.ts I have:

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

packageB/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";
  }
}

The thing with all this is that it works. The imports look nice, its really easy for me to work on. But when I go to build it and publish it I get the following in my 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'm pretty sure this is an issue with my tsconfig. I dont understand all of the settings. Any help would be much appreciated!


Solution

  • As Amir Saleem suggested, setting adding the following to my tsconfig solved my problem:

    "declarationMap": true