使用coffee-script很容易做到這一點。使用來自節點的TypeScript編譯器
var coffee = require('coffee-script');
coffee.compile("a = 1");
//=> '(function() {\n var a;\n\n a = 1;\n\n}).call(this);\n'
有沒有辦法用打字機做到這一點?
使用coffee-script很容易做到這一點。使用來自節點的TypeScript編譯器
var coffee = require('coffee-script');
coffee.compile("a = 1");
//=> '(function() {\n var a;\n\n a = 1;\n\n}).call(this);\n'
有沒有辦法用打字機做到這一點?
看來,現在有一個簡單的解決方案,你可以這樣做:
let ts = require('typescript');
let source = ts.transpileModule('class Test {}', {}).outputText;
這導致:
"use strict";
var Test = (function() {
function Test() {
}
return Test;
}());
注意:ts.transpile已被棄用,因爲這個答案已發佈。這裏有一些來源材料支持上面的答案:https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#transpiling-a-single-file @topek隨時更新您的答案與這個信息。 –
@ShawnErquhart請注意,ts.transpile已被棄用,僅僅因爲它已被更具擴展性的ts.transpileModule所取代。我相應地更新了答案。 –
@AndrewFaulkner是的,這就是爲什麼我鏈接到transpileModule文檔,認爲發佈答案的用戶會更新。感謝那。 –
由於打字稿的NPM模塊不出口任何公共接口,以目前做到這一點的唯一方法是執行tsc
過程。
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已經打開請求公共接口the TypeScript module。
他們在TypeScript 0.9中修復了這個問題嗎? –
他們在TypeScript 2.0測試版中解決了這個問題嗎? –
他們在TypeScript 2.2.1中解決了這個問題嗎? –
檢查this github project通過niutech,它可以在瀏覽器中打字稿代碼轉換爲JS代碼在運行,但我想它可以很容易地進行修改node.js中工作
我在調查支持TypeScript的可能性時發現了它,我的直播firebug-inspired code editor。
better-require可以幫助您實現此目的。
它可以讓你需要()打字稿文件 - 無預編譯需要 - 和一堆其他文件格式(CoffeeScript中,clojurescript,YAML,XML等)
require('better-require')();
var myModule = require('./mymodule.ts');
披露:我寫的更好-要求。
沒有直接回答這個問題,但由於谷歌搜索「從運行打字稿節點直接「調出這個StackOverflow頁面,我想我應該補充說你可以用ts-node做到這一點:
看來TypeScript NPM模塊不會導出任何公共接口。我創建了[追蹤此問題](http://typescript.codeplex.com/workitem/97)。 – joshuapoehls
我也建議看這裏的解決方案: http://stackoverflow.com/questions/12717309/does-typescript-provide-an-explicit-public-api-for-nodejs-module-access – cnp
@ ChristopherPappas很有趣,我也計劃使用它來製作早午餐插件。你有回購的地方我可以檢查出來嗎? –