76

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?

12
  • What do you mean by "cross-plattform manner"?
    – Bergi
    Oct 1, 2012 at 18:19
  • 1
    Is there a library or an application which can transform typescript to js on major platforms (Windows, Linux, OSX)? Oct 1, 2012 at 18:23
  • 1
    your browser is a JS engine so it can compile TS Oct 1, 2012 at 19:25
  • 1
    The question is more generic, because I was curious about all available options. Oct 2, 2012 at 10:58
  • 2
    Not everything can be found on google, especially when it is about a new language or experience people have about a technology. Not asking a question on SO because google exist, is not a valid reason. Oct 3, 2012 at 5:33

12 Answers 12

37

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)

7
  • 1
    Thanks. Ideally, I would prefer to have a java open source framework which compiles typescript to javascript. Since java is already cross platform, it would be possible to use it on any OS. Oct 1, 2012 at 18:46
  • 6
    @AlexObjelean, it is already possible to run the TypeScript compiler on any platform, since it compiles to JS. Oct 1, 2012 at 19:58
  • 2
    Ok, let me put this another way. How would you transform the typescript on the server-side during the build? Oct 1, 2012 at 20:10
  • 3
    npm install -g typescript will put tsc in your PATH on any platform that Node/NPM supports. This includes Windows, OS X, and Linux. It really isn't that hard folks... Oct 3, 2012 at 14:52
  • 1
    @AlexObjelean docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html can be used to run an external command, such as the typescript compiler, from a JVM language such as Java. The typescript tag is hardly appropriate for such a question though. Oct 3, 2012 at 20:46
26

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.

1
  • 2
    Executing tsc.js using WSH (cscript) is not supported anymore (more info). According to this answer the latest version which supports executing via cscript is v1.8.10
    – PolarBear
    Nov 6, 2020 at 8:13
16

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
2
  • 3
    The second command worked for me but I had to install tsc with apt install node-typescript ... on Ubuntu.
    – jgphilpott
    Dec 19, 2021 at 8:39
  • Add the -g option on install or try instead node_modules/.bin/tsc file.ts --outFile file.js in the directory you installed it in.
    – Utopiah
    Apr 24, 2023 at 13:48
12

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.

1
  • 14
    npm install tsc; npx tsc main.ts will output main.js
    – Ray Foss
    Jul 25, 2019 at 17:53
7

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

3
  • 1
    This is something wro4j supports since version 1.6.0: code.google.com/p/wro4j/wiki/AvailableProcessors Jan 17, 2013 at 11:44
  • 1
    @AlexObjelean The wro4j implementation relies on calling out to tsc, which - in turn - relies on npm being installed. This implementation doesn't, as it runs exclusively within Rhino. This is why on the page you linked to, the TypescriptProcessor mentions "not supported on all platforms"
    – Marty Pitt
    Jan 17, 2013 at 12:31
  • that is true. Adding a rhino implementation is on the roadmap. Cheers! Jan 17, 2013 at 13:01
4

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.

2

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?

2
  • 1
    I don't see ECMAScript as an alternative to Java. And I would rather prefer to wrangle JVM to achieve a goal, rather than install node (which is great tool but it is not widely adopted yet). Oct 2, 2012 at 6:45
  • 3
    @AlexObjelean if you're so inclined, it should be possible to get tsc to work under Rhino. You'd have to implement the IIO interface found in typescript.codeplex.com/SourceControl/changeset/view/… Oct 2, 2012 at 9:33
1

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

1

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.

1
1

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!

1

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..
1
  • 1
    File 'typings/tsd.d.ts' not found. May 17, 2019 at 8:46
0

If you are using "firebase -tool" you can use

npm run build

inside your functions directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.