我应该如何使用tsd typings组织打字稿模块/包?(How should i organize typescript modules/packages using tsd typings?)

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

因此,当我在另一个内部安装依赖包时,可能会有一些环境内容的多重定义,例如...root.../typings/node/node.dt 并且它导致转换错误Duplicate identifier 。

我想你们都知道这个问题。 但我花了大约一天谷歌搜索和阅读并尝试了一些不能正常工作的解决方案。 :(

请告诉我一个解决方法。 告诉我你是怎么做的。

I've got quite a large TS-project with numbers of internal libraries that are linked to each other as npm packades. Sometimes in .ts i need to put a references to some ambient or external definitions, provided by TSD, like this: /// <reference path="../../typings/tsd.d.ts" /> Each package includes own .d.ts files referencing to ...root.../typings/tsd.d.ts.

So, when i install a dependency package inside of another one, there can be a multiple definition for some ambient stuff, such as ...root.../typings/node/node.d.t. And it causes transpiling errors Duplicate identifier.

I think you all know that problem. But i've spent about a day googling and reading and trying numbers of solutions that didn't work fine. :(

Please, show me a way out of this. Tell me how you do it.

最满意答案

嗯......一个奇怪的想法来了。 在所有项目中放入一个postinstall钩子脚本,用于更改typings/tsd.d.ts并将重复定义文件的路径替换为父项目,例如: /// <reference path="../../../../typings/node/node.d.ts" />

它很脏但可以正常工作

var fs = require('fs');
var path = require('path');



function searchParentTypingPath(currentPath, moduleName, depth){
    if (!depth) depth = 1;
    if (depth>4) return null;
    try{
        var filePath = currentPath+'/typings/'+moduleName+'/'+moduleName+'.d.ts';
        fs.accessSync(filePath);
        return filePath;
    }
    catch(e){
        return searchParentTypingPath(currentPath+'/..', moduleName, depth+1);
    }
}


module.exports = function (tsdFilePath, verbose){
    var tsdContent = fs.readFileSync(tsdFilePath).toString();
    var resultContent = '';
    tsdContent.split('\n').forEach(function(line){
        var match = line.match(/^\/\/\/\s+<reference path="([\/a-zA-Z0-9_\-.]+)"\s*\/>/);
        if (!match) {
            resultContent+=line+'\n';
            return;
        }
        var modulePath = match[1];
        if (!modulePath.match(/^\w+/)) {// it's not provided by TSD
            resultContent+=line+'\n';
            return;
        }
        var moduleName =  path.basename(modulePath, '.d.ts');

        var parentPath = searchParentTypingPath(path.dirname(tsdFilePath)+'/../..', moduleName);
        resultContent+='/// <reference path="'+(parentPath||modulePath)+'" />\n';
    });
    if (verbose) {
        console.log(resultContent);
        return resultContent;
    }
    fs.writeFileSync(tsdFilePath, resultContent);
}; 
  
 

Hmm... A weird idea has come. Put in all projects a postinstall hook script that changes the typings/tsd.d.ts and replaces paths of duplicating definition files to the parent poject's ones, something like this: /// <reference path="../../../../typings/node/node.d.ts" />

It's dirty but could work fine

var fs = require('fs');
var path = require('path');



function searchParentTypingPath(currentPath, moduleName, depth){
    if (!depth) depth = 1;
    if (depth>4) return null;
    try{
        var filePath = currentPath+'/typings/'+moduleName+'/'+moduleName+'.d.ts';
        fs.accessSync(filePath);
        return filePath;
    }
    catch(e){
        return searchParentTypingPath(currentPath+'/..', moduleName, depth+1);
    }
}


module.exports = function (tsdFilePath, verbose){
    var tsdContent = fs.readFileSync(tsdFilePath).toString();
    var resultContent = '';
    tsdContent.split('\n').forEach(function(line){
        var match = line.match(/^\/\/\/\s+<reference path="([\/a-zA-Z0-9_\-.]+)"\s*\/>/);
        if (!match) {
            resultContent+=line+'\n';
            return;
        }
        var modulePath = match[1];
        if (!modulePath.match(/^\w+/)) {// it's not provided by TSD
            resultContent+=line+'\n';
            return;
        }
        var moduleName =  path.basename(modulePath, '.d.ts');

        var parentPath = searchParentTypingPath(path.dirname(tsdFilePath)+'/../..', moduleName);
        resultContent+='/// <reference path="'+(parentPath||modulePath)+'" />\n';
    });
    if (verbose) {
        console.log(resultContent);
        return resultContent;
    }
    fs.writeFileSync(tsdFilePath, resultContent);
}; 
  
 

更多推荐

ts,typings,root,电脑培训,计算机培训,IT培训"/> <meta name="description&q