![](/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.