简体   繁体   中英

Procedure for generating .d.ts from js file

I am trying to convert my JavaScript file to TypeScript definition (.d.ts).

I am unable to find a tool that does this automatically OR a well defined procedure for converting .js to .ts or .d.ts file.

Could anyone please share the proper procedure for getting .d.ts file for .js file?

Thanks,
Saravanan

Unfortunately it is still a manual process. You can however convert your .js file to a TypeScript .ts file and then the typescript compiler can generate a .d.ts for you using the -d compiler flag. eg the following will give you a foo.d.ts and a foo.js

tsc -d foo.ts --out foo.js

There is disucssion of various tools that you can investigate but none of them are 100% at the moment : https://github.com/borisyankov/DefinitelyTyped/issues/2103#issuecomment-41899391

try the code below. In fact , the following code does not generate .d.ts files, but it generates the tree can write .d.ts file for you to provide a reference , which gives most of the objects of the type of property and function parameters The number of

var Helper = (function () {
    function Helper() {
    }
    Helper.MAX_DUMP_DEPTH = 10;
    Helper.dumpObj = function (obj, name, indent, depth) {
        if (depth > Helper.MAX_DUMP_DEPTH) {
            return indent + name + typeof(obj) +": <Maximum Depth Reached>\n";
        }
        var pre = '';
        pre = (name + '').substr(0, 1);
        if (pre == '_' || pre == '-') {
            return '';
        }
        if (typeof obj == "object") {
            var child = null;
            var output = indent + name + typeof(obj)+"\n";
            indent += "\t";
            for (var item in obj) {
                pre = (item + '').substr(0, 1);
                if (pre == '_' || pre == '-') {
                    continue;
                }
                var temp = obj[item] + '';
                try {
                    child = obj[item];
                }
                catch (e) {
                    child = "<Unable to Evaluate>";
                }
                if (typeof child == "object") {
                    output += Helper.dumpObj(child, item, indent, depth + 1);
                }
                else {
                    output += indent + item + ": " + temp.substr(0, 1 + temp.indexOf(')'))+ typeof(child) + "\n";
                }
            }
            return output;
        }
        else {
            return obj;
        }
    };
    return Helper;
})();


Helper.dumpObj(yourobj,"yourobj","    ",0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM