在 HTML 文档中嵌入 TypeScript 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14015899/
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-09-11 00:57:56  来源:igfitidea点击:

Embed TypeScript code in an HTML document

typescript

提问by Anderson Green

Is it possible to embed TypeScript code in a web page? I want to embed TypeScript code inside script tags, like this (so that it is automatically compiled to Javascript):

是否可以在网页中嵌入 TypeScript 代码?我想在脚本标签中嵌入 TypeScript 代码,就像这样(这样它会自动编译为 Javascript):

<script type = "text/typescript">
    //TypeScript code goes here
</script>

采纳答案by raina77ow

Actually there are several projects that allow you to use TypeScript code like that - TypeScript Compile, ts-htaccess.

实际上,有几个项目允许您像这样使用 TypeScript 代码 - TypeScript Compilets-htaccess

The catch here is that .ts code should be compiled into JavaScript - it can be done either at client-side (slow; the whole TSC should be loaded into the client as well) or at server-side (obviously faster, and it's far easier to leverage the cache on the compiled code).

这里的问题是 .ts 代码应该编译成 JavaScript - 它可以在客户端(慢;整个 TSC 也应该加载到客户端)或在服务器端(显然更快,而且很远)更容易利用编译代码上的缓存)。

回答by basarat

This is a version I wrote that directlyuses the version from Microsoft/TypeScript/master so it always stays up to date: https://github.com/basarat/typescript-script

这是我编写的一个版本,它直接使用 Microsoft/TypeScript/master 的版本,因此它始终保持最新状态:https: //github.com/basarat/typescript-script

You can even point tsto any other TypeScript version you might have and it will work fine

您甚至可以指向ts您可能拥有的任何其他 TypeScript 版本,它会正常工作

回答by Anderson Green

A JavaScript library has already been developed for this purpose - it's called TypeScript Compile, and it allows typescript to be embedded in HTML (as shown above.)

已经为此目的开发了一个 JavaScript 库——它被称为TypeScript Compile,它允许将 typescript 嵌入到 HTML 中(如上所示)。