Transforming TypeScript into JavaScript

前端 未结 11 1244
再見小時候
再見小時候 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:36

    SublimeText2 Trick You can transpile typescript to javascript directly from SublimeText2 (you need node) :

    Create a Typescript.sublime-build file in /Sublime Text 2/Packages/User with this content :

    {
    "cmd": ["tsc", "$file"],
    "selector" : "source.ts",
    "path": "/usr/local/bin:$PATH"
    }
    

    then, now, you can transpile your code with ctrl+B or cmd+B

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

    I have a project which compiles Typescript to Javascript in Java:

    https://github.com/martypitt/typescript4j

    As discussed in other answers, this makes use of Rhino, so has no dependencies on npm or node.

    Here's an example:

    // Instantiate the compiler:
    TypescriptCompiler compiler = new TypescriptCompiler();
    
    // Compile a string:
    String output = compiler.compile("class Greeter { greeting: string; }");
    
    // Or, compile and output to a file:
    compiler.compile(new File("example.ts"), new File('output.js'));
    

    I use it in another project - 'Bakehouse' to perform on-the-fly compilation of typescript resources within Spring environments

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

    Concretely, on the server (assuming your server has Node.js available), you'd simply run:

    node path/to/tsc.js yourFile1.ts yourFile2.ts [etc]
    

    You can run that command without any input filenames to see the command-line help for tsc.js.

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

    This is what worked for me:

    First, installed the typescript node module >> npm install -g typescript. This gives a command line utility tsc.

    Next, tsc gulpfile.ts gulp-config.ts typings/tsd.d.ts

    • this will transpile the gulpfile.ts and gulp-config.ts files to gulpfile.js and gulp-config.js. We supply the typings/tsd.d.ts file as reference for correct transpilation.
    • The typescript node module covers many options >> tsc -h to specify output directory or file, etc..

    Hope this helps.

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

    Short version: use Node if you can. It's becoming unavoidable nowadays.

    Maybe it's not the answer you want, but as everybody mentioned, the compiler is a JS file, so, your options are the options of executing a JS file.

    In Windows, there are 2 obvious ones, Node, and Windows Script Host.

    You know about node already, the other option is a component that comes with all versions of Windows (I think), you can do it like this:

    cscript path/to/tsc.js source-file.ts
    

    You can see all compiler options by just:

    cscript path/to/tsc.js
    

    On Linux I assume you should be able to use (in addition to node):

    • V8 standalone shell, replace node or cscript with v8-shell
    • ExecJS https://github.com/sstephenson/execjs
    • Any other JS runner available on the selected platform (another answer mentioned Rhino for example)

    Update: Another answer suggests the compiler API is only compatible with node and Windows Script Host (cscript tool), so, if correct, then on Linux you'll need Node to compile TypeScript.

    If you are looking for something like apt get tsc (or whatever the Linux/Mac package managers are like), I think there isn't.

    I remember reading somewhere that the I/O is optimized for Node and Windows Script Host, so, if you have problems with options, you'll probably end up with Node if seeking platform independence.

    Update: Another answer here confirms the same about compatibility.

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

    From the command line you can use ts-node:

    npm install ts-node
    

    Then call the command like this:

    tsc file.ts --outFile file.js
    
    0 讨论(0)
提交回复
热议问题