Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在节点库中添加typings引用_Node.js_Typescript - Fatal编程技术网

Node.js 在节点库中添加typings引用

Node.js 在节点库中添加typings引用,node.js,typescript,Node.js,Typescript,我用纯JS创建了一个NodeJS库,并添加了打字,在另一个项目中使用npm | thread安装该库时,打字效果很好 但是,当在其自己的目录中引用库(例如用于测试)时,WebStorm和VS代码都无法识别代码完成/智能感知的类型(悬停在方法上表示找不到它们的定义) 我的库(/index.js): 打字(/index.d.ts): 我试着像这样在我的测试文件(test/index.js)中添加引用,但没有解决问题: /// <reference types="../index.d.ts" /

我用纯JS创建了一个NodeJS库,并添加了打字,在另一个项目中使用
npm | thread
安装该库时,打字效果很好

但是,当在其自己的目录中引用库(例如用于测试)时,WebStorm和VS代码都无法识别代码完成/智能感知的类型(悬停在方法上表示找不到它们的定义)

我的库(
/index.js
):

打字(
/index.d.ts
):

我试着像这样在我的测试文件(
test/index.js
)中添加引用,但没有解决问题:

/// <reference types="../index.d.ts" /> 
const myLib = require('../')
//
常量myLib=require(“../”)

问题来自
声明模块“我的模块”
。当输入位于包中时,无需命名模块。此外,非标准导出的语法为在您的案例中不是必需的,因为您的包包含常规的命名导出。只需将其导出即可。我建议:

// index.d.ts
export declare function port(number: number): void;
// … you can add other named exports like:
export declare function user(name: string): void;
// or even (it's equivalent):
export declare const user2: (name: string) => void;
// etc.
如果文件由
package.json
文件中的
类型“
引用,则此类型将作为包类型使用。导入
/index
文件时,它也会起作用,因为它共享相同的名称


注意:TypeScript遵循。

这似乎现在可以工作了,但是如果我的lib中有多个函数,并且我的lib有一个
接口,那么会发生什么:
{port(number:number):void,user(name:string):void}
很好,就是这样。谢谢
/// <reference types="../index.d.ts" /> 
const myLib = require('../')
// index.d.ts
export declare function port(number: number): void;
// … you can add other named exports like:
export declare function user(name: string): void;
// or even (it's equivalent):
export declare const user2: (name: string) => void;
// etc.