簡體   English   中英

在npm的打字稿中打包環境模塊的正確方法

[英]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包“get-subject”作為依賴

做正常的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM