Skip to content

Instantly share code, notes, and snippets.

@suisho suisho/example.d.ts
Created Nov 12, 2012

What would you like to do?
typescript compiler that custom library
$ tsc sample.ts
$ npm install typescript
$ node my_ts_compiler.js sample.ts
declare function exampleFunc() : int;
var fs = require('fs');
function createCompiler(outfile, outerr, libFiles){
// npm install typescript 前提
var nodeModulePath = 'node_modules/typescript/bin/typescript.js';
require('vm').runInThisContext(fs.readFileSync(nodeModulePath), 'typescript.js');
var ts = TypeScript;
var compiler = new ts.TypeScriptCompiler(outfile, outerr);
var srcCode = fs.readFileSync(file).toString();
compiler.addUnit(srcCode,file, false);
return compiler;
function complieTs(src, libFiles){
var outfile = {
source: '',
Write: function(s) {this.source += s;},
WriteLine: function(s) {this.source += s + "\r\n";},
Close: function(){}
var outerr = {
Write: function(s) {},
WriteLine: function(s) {},
Close: function() {},
var compiler = createCompiler(outfile,outerr,libFiles);
var srcCode = fs.readFileSync(src).toString();
compiler.emit(true, function(){
return outfile;
var src = process.argv[2];
var libFiles = [
complieTs(src, libFiles);
var loadExample = exampleFunc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.