Imagine creating an npm package named get-subject with a source file called src/index.ts
. This is how the code looks:
import { ReplaySubject } from 'rx';
export default function getSubject() {
return new ReplaySubject(1);
}
The package includes package.json:
"main": "lib/index.js",
"typings": "lib/index.d.ts",
As well as tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"outDir": "lib"
},
"files": [
"src/index.ts",
"node_modules/rx/ts/rx.all.d.ts"
]
}
Running tsc -d
generates files in the lib/
directory:
lib/index.js
lib/index.d.ts
The content of lib/index.d.ts
is:
import { ReplaySubject } from 'rx';
export default function getSubject(): ReplaySubject<{}>;
Integrating npm package "get-subject" as a dependency
To use this package, simply run npm i
and incorporate it into your code like so:
import getSubject from 'ts-npm-package';
getSubject()
However, upon running tsc
, you might encounter this error:
node_modules/get-subject/lib/index.d.ts(1,31): error TS2307: Cannot find module 'rx'.
If you add node_modules/rx/ts/rx.all.d.ts
to the files
property in tsconfig.json, tsc
will work. But is this the most optimal solution? Is there a way to simplify this process for the package users?