Transforming TypeScript into JavaScript

前端 未结 11 1245
再見小時候
再見小時候 2020-12-08 02:01

I\'m wondering how is it possible to transform the TypeScript into JavaScript in a cross platform manner. I\'m aware about availability of node package manager for typescrip

相关标签:
11条回答
  • 2020-12-08 02:48

    I've been playing around with this, and can compile TypeScript with javascript with the following code:

    <script src=typescript.js></script>
    
    <script>
    
    var scriptText = ""
            + "/// <reference path=\"test.ts\"/>" + "\r\n"
            + "class Car {"
            + "     constructor (private name: string) { } "
            + "     getName() { "
            + "         var juice = new Juice();"
            + "         return name; "
            + "     } "
            + "} "
            + "var car = new Car('Subaru Impreza');"
            + "console.log(car.getName());";
    
    
    
    
    
    var TextWriter = function () { };
    
    TextWriter.prototype = {
    collected: '',
    
    Write: function (sc) {
        this.collected += sc;
    },
    WriteLine: function(sc) {
        this.collected += sc + '\n';
    },
    toString: function() {
        return this.collected;
    }
    };
    
    var output = new TextWriter();
    var tsc = new TypeScript.TypeScriptCompiler(output);
    
    var script = tsc.addUnit(scriptText, "");
    
    tsc.emit();
    
    console.log(output.toString());
    
    </script>
    

    It's not exactly ideal though. I'm trying to get something running so I can convert TypeScript to JS within C# (using Javascript .NET), but i'm getting a stack overflow on the ts.addUnit call.

    0 讨论(0)
  • 2020-12-08 02:49

    If it's Java that you need to target then you could run tsc.js with the Rhino engine as part of your build process.

    0 讨论(0)
  • 2020-12-08 02:58

    You probably don't wanna use ts-node, because it is slow, instead follow following steps for fast .ts files compilation (Make sure node is installed):

    1. npm i -D @types/node typescript nodemon

    2. npx tsconfig.json and select node from the list. You are free to modify it as per your needs.

    3. Create a file names src/index.ts in your project root.

    4. Then in your package.json, add the following 2 scripts:

      "scripts": { "watch": "tsc -w", "dev": "nodemon dist/index.js" },

    5. Then use:

      npm run watch

      npm run dev

    And, it will automatically look for changes in .ts files and you can see the compiled file output in the terminal as you go!

    0 讨论(0)
  • 2020-12-08 02:59

    To compile ts -> js: node is available for all common platforms, so I fail to see why you'd want to have a tsc.java when you already have a tsc.js. Installing node is no big deal. In fact, it's easier than Java.

    Once you have your proj.js file, you can then copy it to which ever deployment platform you wish to use.

    From my point of view, JavaScript - or more accurately ECMAScript is an alternative to Java. So I'm happy that I don't have to wrangle JVM etc to use the tool. But if you prefer Java, then why even bother with JS?

    0 讨论(0)
  • 2020-12-08 03:00

    The TypeScript compiler is built in TypeScript, and hence is available as a JS file (tsc.js) that can be run using just about any ES3-compiliant VM or JS implementation.

    That said, the compiler's current file I/O infrastructure only supports Node and Windows Scripting Host file APIs. If you'd like to recommend for support for another environment, feel free to reach out to the team at our GitHub site (Formerly CodePlex)

    0 讨论(0)
提交回复
热议问题