将 TypeScript 转换为 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12678716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:00:06  来源:igfitidea点击:

Transforming TypeScript into JavaScript

javascripttypescript

提问by Alex Objelean

I'm wondering how is it possible to transform the TypeScriptinto 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?

我想知道如何以跨平台的方式将TypeScript转换为 JavaScript。我知道typescript 节点包管理器的可用性,但是还有其他可以在服务器端使用的替代方法吗?

采纳答案by Meligy

Short version:use Node if you can. It's becoming unavoidable nowadays.

简短版本:如果可以,请使用 Node。这在今天变得不可避免。

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.

也许这不是你想要的答案,但正如大家所说,编译器是一个 JS 文件,所以,你的选项是执行一个 JS 文件的选项。

In Windows, there are 2 obvious ones, Node, and Windows Script Host.

在 Windows 中,有两个明显的,Node 和 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:

您已经了解 node,另一个选项是所有版本的 Windows 附带的组件(我认为),您可以这样做:

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):

在 Linux 上,我假设您应该能够使用(除了节点):

  • V8 standalone shell, replace nodeor cscriptwith v8-shell
  • ExecJS https://github.com/sstephenson/execjs
  • Any other JS runner available on the selected platform (another answer mentioned Rhino for example)
  • V8 独立外壳,替换nodecscript使用v8-shell
  • ExecJS https://github.com/sstephenson/execjs
  • 所选平台上可用的任何其他 JS 运行器(例如另一个提到 Rhino 的答案)

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.

更新:另一个答案表明编译器 API 仅与 node 和 Windows Script Host(cscript 工具)兼容,因此,如果正确,那么在 Linux 上您将需要 Node 来编译 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.

如果您正在寻找类似的东西apt get tsc(或任何 Linux/Mac 包管理器),我认为没有。

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.

我记得在某处读到过 I/O 针对 Node 和 Windows Script Host 进行了优化,因此,如果您在选项方面遇到问题,如果寻求平台独立性,您可能最终会使用 Node。

Update:Another answer here confirms the same about compatibility.

更新:这里的另一个答案证实了兼容性。

回答by Joe Pamer

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.

TypeScript 编译器内置于 TypeScript 中,因此可以作为 JS 文件 ( tsc.js) 使用,几乎可以使用任何符合 ES3 的 VM 或 JS 实现来运行。

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)

也就是说,编译器当前的文件 I/O 基础结构仅支持 Node 和 Windows Scripting Host 文件 API。如果您想推荐其他环境的支持,请随时与我们GitHub 站点(以前的CodePlex的团队联系

回答by Ryan Cavanaugh

Concretely, on the server (assuming your server has Node.js available), you'd simply run:

具体来说,在服务器上(假设您的服务器有可用的 Node.js),您只需运行:

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.

您可以在没有任何输入文件名的情况下运行该命令以查看 tsc.js 的命令行帮助。

回答by Marty Pitt

I have a project which compiles Typescript to Javascript in Java:

我有一个用 Java 将 Typescript 编译为 Javascript 的项目:

https://github.com/martypitt/typescript4j

https://github.com/martypitt/typescript4j

As discussed in other answers, this makes use of Rhino, so has no dependencies on npmor node.

正如其他答案中所讨论的,这使用了 Rhino,因此不依赖于npmnode

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

我在另一个项目中使用它- 'Bakehouse'在 Spring 环境中执行打字稿资源的即时编译

回答by Christopher Cook

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.

如果您需要以 Java 为目标,那么您可以在构建过程中使用 Rhino 引擎运行 tsc.js。

回答by CyberFonic

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.

编译 ts -> js: node 可用于所有常见平台,所以我不明白为什么当你已经有一个 tsc.js 时你还想要一个 tsc.java。安装节点没什么大不了的。事实上,它比Java更容易。

Once you have your proj.js file, you can then copy it to which ever deployment platform you wish to use.

拥有 proj.js 文件后,您可以将其复制到您希望使用的任何部署平台。

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?

在我看来,JavaScript——或者更准确地说 ECMAScript 是 Java 的替代品。所以我很高兴我不必为使用该工具而纠结 JVM 等。但是,如果您更喜欢 Java,那为什么还要为 JS 烦恼呢?

回答by Ariel Monaco

From the command line you can use ts-node:

您可以从命令行使用 ts-node:

npm install ts-node

Then call the command like this:

然后像这样调用命令:

tsc file.ts --outFile file.js

回答by k33g_org

SublimeText2 TrickYou can transpile typescript to javascript directly from SublimeText2 (you need node) :

SublimeText2 技巧您可以直接从 SublimeText2 将打字稿转换为 javascript(您需要节点):

Create a Typescript.sublime-buildfile in /Sublime Text 2/Packages/Userwith this content :

使用此内容创建一个Typescript.sublime-build文件/Sublime Text 2/Packages/User

{
"cmd": ["tsc", "$file"],
"selector" : "source.ts",
"path": "/usr/local/bin:$PATH"
}

then, now, you can transpile your code with ctrl+B or cmd+B

然后,现在,您可以使用 ctrl+B 或 cmd+B 转译您的代码

回答by eoleary

I've been playing around with this, and can compile TypeScript with javascript with the following code:

我一直在玩这个,并且可以使用以下代码使用 javascript 编译 TypeScript:

<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.

但这并不完全理想。我正在尝试运行一些东西,以便我可以在 C# 中将 TypeScript 转换为 JS(使用 Javascript .NET),但是我在 ts.addUnit 调用中遇到堆栈溢出。

回答by Akash

This is what worked for me:

这对我有用:

First, installed the typescriptnode module>> npm install -g typescript. This gives a command line utility tsc.

首先,安装typescript节点模块>> npm install -g typescript。这提供了一个命令行实用程序tsc

Next, tsc gulpfile.ts gulp-config.ts typings/tsd.d.ts

下一个, tsc gulpfile.ts gulp-config.ts typings/tsd.d.ts

  • this will transpilethe gulpfile.tsand gulp-config.tsfiles to gulpfile.jsand gulp-config.js. We supply the typings/tsd.d.tsfile as reference for correct transpilation.
  • The typescriptnode modulecovers many options >> tsc -hto specify output directory or file, etc..
  • 这将transpilegulpfile.tsgulp-config.ts文件gulpfile.jsgulp-config.js。我们提供该typings/tsd.d.ts文件作为正确翻译的参考。
  • typescript节点模块涵盖了许多选项>>tsc -h指定输出目录或文件,等等

Hope this helps.

希望这可以帮助。