如何使用npm模块的自定义类型定义?

时间:2017-03-06 03:58:54

标签: typescript typescript2.2

我已经为npm包react-sortable-tree编写了类型定义。我无法想象我应该如何消费它。这是我尝试过的。

我将定义文件命名为react-sortable-tree/index.d.ts(遵循Typescript documentation中给出的说明)并将文件名添加到我的tsconfig.json。 Typescript编译器(版本2.2.1)找不到模块的定义。我尝试了module resolution methods ClassicNode但没有成功。

将文件重命名为react-sortable-tree.d.ts后,编译器会使用分辨率方法Classic识别该文件。但是,npm包中包含类型定义文件的其他模块(例如,redux),编译器无法找到这些包的定义。

使用解析方法Node时,编译器会查找所有包的类型定义,但我已定义的包除外。对于react-sortable-tree包,编译器会解析包附带的实际Javascript文件的路径,并忽略我的定义。因此,不会进行类型检查。

如何编写我的定义,在哪里放置它,以及我应该指定哪些编译器选项以便编译器考虑我的类型定义?

1 个答案:

答案 0 :(得分:1)

这是我对npm模块$ cat dat/file.txt 523 $ ./bin/addintoarray dat/file.txt 4 enter 4 integers integer[ 1] : 400 integer[ 2] : 500 integer[ 3] : 600 integer[ 4] : 700 sorted array before inserting value from file: a[ 0]: 700 a[ 1]: 600 a[ 2]: 500 a[ 3]: 400 value from file: 523 sorted array after inserting value from file: a[ 0]: 700 a[ 1]: 600 a[ 2]: 523 a[ 3]: 500 a[ 4]: 400 的声明文件的设置:

tsconfig:

jison

./ SRC /定义/ jison.d.ts:

"include": [
    "./src/definitions/jison.d.ts",
    "./src/index.ts"
 ]
相关问题