问题描述
假设我编写了一个名为 get-subject 的npm程序包,其源代码为src/index.ts
,如下所示:
Assuming I have wrote a npm package called get-subject with source src/index.ts
like this:
import { ReplaySubject } from 'rx';
export default function getSubject() {
return new ReplaySubject(1);
}
我有package.json:
I have package.json:
"main": "lib/index.js",
"typings": "lib/index.d.ts",
和tsconfig.json:
And 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/
中生成文件:
Now I run tsc -d
and it generates files in 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包编写一些代码:
Time to use npm package "get-subject" as a dependency
Do normal npm i
. Wrote some code using get-subject package:
import getSubject from 'ts-npm-package';
getSubject()
但是当我运行tsc
时,它会告诉我:
But when I run tsc
, it tells me:
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
将起作用. 但这是理想的解决方案吗?我是否可以提供一种方法,使软件包用户不必自己找出缺少的内容?
If I include node_modules/rx/ts/rx.all.d.ts
(I use npm@3 so the Rx dependency is not nested under get-subject
) in files
property in tsconfig.json. tsc
will work. But this is the ideal solution? Could I provide a way so that package user won't have to figure out what's missing by themselves?
推荐答案
是的.直到rx.js附带其.d.ts
文件位于其.js
文件旁边 right 为止.
Yes. Until rx.js ships with its .d.ts
files being right next to its .js
files.
这篇关于正确的方法将环境模块打包在nscript的打字稿中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!