![](/img/trans.png)
[英]What is the correct way for a TypeScript NPM package to depend on an ambient module?
[英]Correct way to package ambient module in typescript for npm
假设我已经编写了一个名为get-subject的npm包,其源代码为src/index.ts
如下所示:
import { ReplaySubject } from 'rx';
export default function getSubject() {
return new ReplaySubject(1);
}
我有package.json:
"main": "lib/index.js",
"typings": "lib/index.d.ts",
和tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"outDir": "lib"
},
"files": [
"src/index.ts",
"node_modules/rx/ts/rx.all.d.ts"
]
}
现在我运行tsc -d
并在lib/
生成文件:
lib/index.js
lib/index.d.ts
而lib/index.d.ts
看起来像
import { ReplaySubject } from 'rx';
export default function getSubject(): ReplaySubject<{}>;
做正常的npm i
。 使用get-subject包写了一些代码:
import getSubject from 'ts-npm-package';
getSubject()
但是当我运行tsc
,它会告诉我:
node_modules/get-subject/lib/index.d.ts(1,31): error TS2307: Cannot find module 'rx'.
如果我在tsconfig.json的files
属性中包含node_modules/rx/ts/rx.all.d.ts
(我使用npm @ 3,因此Rx依赖关系不嵌套在get-subject
)。 tsc
会起作用。 但这是理想的解决方案吗? 我可以提供一种方法,使包裹用户不必自己弄清楚缺少什么吗?
但这是理想的解决方案吗?
是。 直到rx.js附带了.d.ts
文件是紧挨其.js
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.