15

It's pretty easy to do this with coffee-script.

var coffee = require('coffee-script');
coffee.compile("a = 1");
//=> '(function() {\n  var a;\n\n  a = 1;\n\n}).call(this);\n'

Is there a way to do this with typescript?

Edit: also posted on codeplex

3
  • It appears that the TypeScript NPM module doesn't export any public interface. I've created an issue to track this. Commented Oct 4, 2012 at 15:11
  • I would also recommend looking at the solution here: stackoverflow.com/questions/12717309/… Commented Oct 4, 2012 at 22:05
  • @ChristopherPappas that's funny, I was also planning on using this to make a brunch plugin. You have a repo somewhere I can check out? Commented Oct 4, 2012 at 22:37

6 Answers 6

16

It seems that nowadays there is a simpler solution, you can do:

let ts = require('typescript');
let source = ts.transpileModule('class Test {}', {}).outputText;

This results in:

"use strict";
var Test = (function () {
    function Test() {
    }
    return Test;
}());
Sign up to request clarification or add additional context in comments.

3 Comments

Note: ts.transpile has been deprecated since this answer was posted. Here's some source material to back the answer above: github.com/Microsoft/TypeScript/wiki/… @topek feel free to update your answer with this info.
@ShawnErquhart Note that ts.transpile is deprecated only because it has been superseded by the more extensible ts.transpileModule. I updated the answer accordingly.
@AndrewFaulkner yeah that's why I linked to documentation on transpileModule, thought the user who posted the answer would update. Thanks for that.
8

Since TypeScript's NPM module doesn't export any public interface, the only way to do this currently is to execute the tsc process.

var exec = require('child_process').exec;

var child = exec('tsc main.ts',
                function(error, stdout, stderr) {
                    console.log('stdout: ' + stdout);
                    console.log('stderr: ' + stderr);
                    if (error !== null) {
                      console.log('exec error: ' + error);
                    }
                });

An issue has been opened to request a public interface for the TypeScript module.

4 Comments

Did they fix this in TypeScript 0.9?
Did they fix this in TypeScript 2.0 beta?
Did they fix this in TypeScript 2.2.1?
Did they fix this in TypeScript 2.4 RC? (jk, they fixed it much earlier)
8

better-require could help you achieve this.

It lets you require() typescript files - no pre-compilation needed - and a bunch of other file formats (coffeescript, clojurescript, yaml, xml, etc.)

require('better-require')();
var myModule = require('./mymodule.ts');

Disclosure: I wrote better-require.

Comments

6

Check this github project by niutech, it can convert TypeScript code to JS code on the fly in browser, but I guess it can be easily be modified to work in node.js.

I found it while I'm investigating the possibility of support TypeScript in my live, firebug-inspired code editor.

Comments

4

Not answering the question directly, but since Googling for "run TypeScript from node directly" brings up this StackOverflow page, I figure I should add that you're able to do this with ts-node: https://github.com/TypeStrong/ts-node

Comments

0

Official documentation about how to use TypeScript transpiler API to generate JavaScript source from a .ts file:

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-transpiling-a-single-file

Official documentation about how to use TypeScript compiler API to compile a .ts file or a TS project to

https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#user-content-a-minimal-compiler

(the later doesn't answer the original question but is very common to access /modify the AST and then transpile to object language so it could be usefull)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.