Answer the question
In order to leave comments, you need to log in
How to properly organize modules using typescript and TSD (DefinetelyTyped) definitions?
I have a fairly large TS project with a bunch of interrelated libraries screwed together as NPM dependencies. In .ts files, in some places I have links to external .d.ts definitions, which TSD kindly provides me - like this: /// <reference path="../../typings/tsd.d.ts" />
Well, as a result, each library is built in js and is accompanied by .d.ts definitions that include links to ...root.../typings/tsd.d.ts
.
Further, when I install dependent modules with all this typed nonsense, I end up with duplicate definitions of things like ...root.../typings/node/node.d.t
. Well, when compiling, a myriad of errors fall Duplicate identifier
.
I think that for those who can help me, a more detailed description is unnecessary. :) The problem is widespread and understandable. It's not clear how to properly break it down. I have already spent a whole day googling and trying out all sorts of solutions. They either do not work, or harm, or I did not understand them. So please explain to me as a child how modules on ts should be organized.
Answer the question
In order to leave comments, you need to log in
Well, I came up with one option ... dumb, but still.
in package.json in scripts, hang a postinstall hook that will run through tsd.ts and fix links to
/// <reference path="../../../../typings/node/node.d.ts" />
if they duplicate the definitions of the parent project. 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);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question