Convert generated JavaScript back to TypeScript

Topics: General, Language Specification
Feb 15, 2014 at 9:09 AM
Edited Feb 15, 2014 at 9:22 AM
Is there a way to convert generated .js JavaScript back to the origin .ts-Format?

I mean something like a conversion of:
var Greeter = (function () {
    function Greeter() {
    }
    Greeter.prototype.greet = function (g) {
        return "Hello " + g.s + "!";
    };
    return Greeter;
})();
back to:
class Greeter {
    
    s:string;
    
    greet(g:Greeter) {
        return "Hello " + g.s + "!";
    }
}
If there is not, will it be available in the next versions of typescript?

I know that the type information will be lost while translating .ts to .js, but maybe it can be memorized by comments:
///#class Greeter
var Greeter /*:class*/ = (function () {

    ///#s:string

    function /*:constructor*/ Greeter() {
    }
    Greeter.prototype.greet = function (g/*:Greeter*/) {
        return "Hello " + g.s + "!";
    };
    return Greeter;
})();
(or with a different syntax - you are quite smarter :D)

It would be great to open a *.js file in Visual Studio and a question pops up: "TypeScript detected - Would you like to recover the original TypeScript Code?"

I would like to save the files in the .js format and edit them in the .ts format.
Coordinator
Feb 24, 2014 at 2:59 PM
We've talked a bit about extending the compiler so that it can produce JSDoc output in the compile .js file. If this were sufficiently detailed, it might be possible to use (or write) a JSDoc'd JS to TS converter that could allow you to roundtrip code back to TypeScript.

To do what you're talking about now, as an alternate approach you could use source maps that point back to the original TypeScript from the JavaScript files. We do this, for example, to enable debugging directly on the .ts files.