I originally wrote this answer when TypeScript was still hot-off-the-presses. (我最初是在TypeScript仍然热销时写这个答案的。) Five years later, this is an OK overview, but look at Lodewijk's answer below for more depth (五年后,这是一个不错的概述,但请查看以下Lodewijk的答案以获取更多深度)
1000ft view... (1000英尺查看...)
TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. (TypeScript是JavaScript的超集,主要提供可选的静态类型,类和接口。) One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code . (最大的好处之一是使IDE能够提供更丰富的环境,以便在键入代码时发现常见错误。)
To get an idea of what I mean, watch Microsoft's introductory video on the language. (要了解我的意思,请观看有关该语言的Microsoft入门视频 。)
For a large JavaScript project, adopting TypeScript might result in more robust software, while still being deployable where a regular JavaScript application would run. (对于大型JavaScript项目,采用TypeScript可能会导致软件更强大,同时仍可以在运行常规JavaScript应用程序的地方进行部署。)
It is open source, but you only get the clever Intellisense as you type if you use a supported IDE. (它是开源的,但是如果您使用受支持的IDE,则只有在键入时才能获得聪明的Intellisense。) Initially, this was only Microsoft's Visual Studio (also noted in blog post from Miguel de Icaza ). (最初,这只是Microsoft的Visual Studio(也在Miguel de Icaza的博客文章中提到)。) These days, other IDEs offer TypeScript support too . (如今 , 其他IDE也提供TypeScript支持 。)
Are there other technologies like it? (还有其他类似的技术吗?)
There's CoffeeScript , but that really serves a different purpose. (有CoffeeScript ,但这确实有不同的用途。) IMHO, CoffeeScript provides readability for humans, but TypeScript also provides deep readability for tools through its optional static typing (see this recent blog post for a little more critique). (恕我直言,CoffeeScript为人类提供了可读性,但TypeScript还通过其可选的静态类型为工具提供了深层的可读性(有关更多批评,请参阅此最新博客文章 )。) There's also Dart but that's a full on replacement for JavaScript (though it can produce JavaScript code ) (还有Dart,但可以完全替代JavaScript(尽管它可以生成JavaScript代码 ))
Example (例)
As an example, here's some TypeScript (you can play with this in the TypeScript Playground ) (例如,这是一些TypeScript(您可以在TypeScript Playground中使用它 ))
class Greeter {
greeting: string;
constructor (message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
And here's the JavaScript it would produce (这是它将产生的JavaScript)
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
Notice how the TypeScript defines the type of member variables and class method parameters. (注意TypeScript如何定义成员变量和类方法参数的类型。) This is removed when translating to JavaScript, but used by the IDE and compiler to spot errors, like passing a numeric type to the constructor. (转换为JavaScript时已将其删除,但IDE和编译器使用它来发现错误,例如将数字类型传递给构造函数。)
It's also capable of inferring types which aren't explicitly declared, for example, it would determine the greet()
method returns a string. (它也能够推断出未明确声明的类型,例如,它将确定greet()
方法返回一个字符串。)
Debugging TypeScript (调试TypeScript)
Many browsers and IDEs offer direct debugging support through sourcemaps. (许多浏览器和IDE通过Sourcemap提供直接调试支持。) See this Stack Overflow question for more details: Debugging TypeScript code with Visual Studio (有关更多详细信息,请参见此堆栈溢出问题: 使用Visual Studio调试TypeScript代码)
Want to know more? (想知道更多?)
I originally wrote this answer when TypeScript was still hot-off-the-presses. (我最初是在TypeScript仍然热销时写这个答案的。) Check out Lodewijk's answer to this question for some more current detail. (查看Lodewijk对这个问题的答案,以获取更多当前的详细信息。)