Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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 我应该如何使用tsd类型组织typescript模块/包?_Node.js_Typescript_Definitelytyped - Fatal编程技术网

Node.js 我应该如何使用tsd类型组织typescript模块/包?

Node.js 我应该如何使用tsd类型组织typescript模块/包?,node.js,typescript,definitelytyped,Node.js,Typescript,Definitelytyped,我有一个相当大的TS项目,有许多内部库,它们作为npm包相互链接。 有时在.ts中,我需要引用TSD提供的一些环境或外部定义,如下所示: // 每个包都包含自己的.d.ts文件,引用…root…/typings/tsd.d.ts 因此,当我在另一个依赖项包中安装一个依赖项包时,可能会有一些环境内容的多个定义,例如…root…/typings/node/node.d.t。它会导致传输错误重复标识符 我想你们都知道这个问题。但我已经花了大约一天的时间在谷歌上搜索、阅读和尝试了大量不太奏效的解决方案(

我有一个相当大的TS项目,有许多内部库,它们作为npm包相互链接。 有时在.ts中,我需要引用TSD提供的一些环境或外部定义,如下所示:
//
每个包都包含自己的.d.ts文件,引用
…root…/typings/tsd.d.ts

因此,当我在另一个依赖项包中安装一个依赖项包时,可能会有一些环境内容的多个定义,例如
…root…/typings/node/node.d.t
。它会导致传输错误
重复标识符

我想你们都知道这个问题。但我已经花了大约一天的时间在谷歌上搜索、阅读和尝试了大量不太奏效的解决方案(


请告诉我一条出路。告诉我你是怎么做到的。

嗯……一个奇怪的想法出现了。 在所有项目中放入一个
postnstall
hook脚本,该脚本更改
typings/tsd.d.ts
,并将复制定义文件的路径替换为父项目的路径,如下所示:
/

它很脏,但可以很好地工作

var fs=require('fs');
var path=require('path');
函数searchParentTypingPath(currentPath、moduleName、depth){
如果(!depth)depth=1;
如果(深度>4),则返回null;
试一试{
var filePath=currentPath+'/typings/'+moduleName+'/'+moduleName+'.d.ts';
fs.accessSync(文件路径);
返回文件路径;
}
捕获(e){
返回searchParentTypingPath(currentPath+'/..',moduleName,深度+1);
}
}
module.exports=函数(tsdFilePath,详细){
var tsdContent=fs.readFileSync(tsdFilePath.toString();
var resultContent='';
tsdContent.split('\n').forEach(函数(行){
var match=line.match(/^\/\/\/\s+/);
如果(!匹配){
结果内容+=行+'\n';
返回;
}
var modulePath=匹配[1];
如果(!modulePath.match(//^\w+/){//,则TSD不提供该选项
结果内容+=行+'\n';
返回;
}
var moduleName=path.basename(modulePath,.d.ts');
var parentPath=searchParentTypingPath(path.dirname(tsdFilePath)+'/....',moduleName);
结果内容+='//\n';
});
如果(详细){
console.log(resultContent);
返回结果内容;
}
fs.writeFileSync(tsdFilePath,resultContent);
};