I have a NPM package written in Typescript (let's call it libd
) that offers some classes and types. They are exported in a index.ts
file like this:
export { ClassA } from "./ClassA";
export { TypeB } from "./TypeB";
The ClassA
is using an external dependency (let's call it depc
) that is set to peerDependencies in the package.json
. The TypeB
is just a type without any external dependencies.
In the package.json
the main field is set to dist/index.js
(where it is transpiled to) and types are set to dist/index.d.ts
.
The tsconfig.json
of the libd
package looks like this:
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"declaration": true,
"outDir": "dist",
"rootDir": "src",
"inlineSources": true,
"inlineSourceMap": true
}
}
Now I im consume the TypeB
in another library (let's call it libe
) without importing ClassA
and without having the depc
dependency installed. The libd
library is installed via NPM and the type is imported like this:
import { TypeB } from "libd";
The tsconfig.json
of libe
is similar to the one from libd
.
Transpiling this gives me the error TS2307: Cannot find module 'depc'
.
Now I understand, if I would use ClassA
I must install depc
since it imports it. But I'm only using TypeB
that does not require any dependencies and sometimes types come with the original package.
I could fix this problem by installing @types/depc
and Typescript would be happy. Code should work at runtime, since no actual usage of depc
comes from libe
. But I would rather not install useless packages.
Setting "skipLibCheck": true
in the tsconfig.json
of libe
would also fix the tsc error but I am not sure what negative impacts this would have.
So my question is: Why does typescript check a class from dependency that I am not importing? Is it, because I have bundled all exports of the libd
package in an index.ts
file and all imports of those are going through this index file?
Can I resolve this without separating TypeB
and ClassA
into different packages? That would make them very small packages and for ~80% of the time they would be installed both.