Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming TypeScript into JavaScript

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 typescript, but are there any other alternatives which can be used on the server side?

like image 630
Alex Objelean Avatar asked Oct 01 '12 18:10

Alex Objelean


People also ask

How TypeScript is compiled to JavaScript?

TypeScript provides a command-line utility tsc that compiles (transpiles) TypeScript files ( . ts ) into JavaScript. However, the tsc compiler (short for TypeScript compiler) needs a JSON configuration file to look for TypeScript files in the project and generate valid output files at a correct location.

How do I convert TSX to JavaScript?

How to compile TypeScript to JavaScript: Fill in the editor above with your typescript code. You can Drag and drop your file, copy and paste your code, or type directly into the editor above. Click on "Convert" button to execute the compilation of your source code.

What converts TypeScript to JavaScript in angular?

TypeScript is a primary language for Angular application development. It is a superset of JavaScript with design-time support for type safety and tooling. Browsers can't execute TypeScript directly. Typescript must be "transpiled" into JavaScript using the tsc compiler, which requires some configuration.


2 Answers

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)

like image 158
Joe Pamer Avatar answered Sep 27 '22 15:09

Joe Pamer


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.

like image 20
Meligy Avatar answered Sep 27 '22 15:09

Meligy