问题描述
您能描述一下TypeScript语言是什么吗?
Can you please describe what the TypeScript language is?
JavaScript或可用库无法做什么,这会让我有理由考虑它?
What can it do that JavaScript or available libraries cannot do, that would give me reason to consider it?
推荐答案
1000英尺视图...
是JavaScript的超集,主要提供可选的静态类型,类和接口。一个很大的好处是让IDE能够提供更丰富的环境来在输入代码时发现常见错误。
1000ft view...
TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code.
获得一个想法我的意思是,观看有关该语言的。
To get an idea of what I mean, watch Microsoft's introductory video on the language.
对于大型JavaScript项目,采用TypeScript可能会产生更强大的软件,同时仍可部署常规JavaScript应用程序。
For a large JavaScript project, adopting TypeScript might result in more robust software, while still being deployable where a regular JavaScript application would run.
它是开源的,但如果您使用支持的IDE,则只能在键入时获得聪明的Intellisense。最初,这只是微软的Visual Studio(也来自)。目前,。
It is open source, but you only get the clever Intellisense as you type if you use a supported IDE. Initially, this was only Microsoft's Visual Studio (also noted in blog post from Miguel de Icaza). These days, other IDEs offer TypeScript support too.
,但这确实有不同的用途。恕我直言,CoffeeScript为人类提供了可读性,但TypeScript还通过其可选的静态类型为工具提供了深度可读性(请参阅此,以获得更多批评)。还有,但这完全取代了JavaScript(虽然它)
There's CoffeeScript, but that really serves a different purpose. 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). There's also Dart but that's a full on replacement for JavaScript (though it can produce JavaScript code)
举个例子,这里有一些TypeScript(你可以在)
As an example, here's some TypeScript (you can play with this in the TypeScript Playground)
class Greeter {
greeting: string;
constructor (message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
这是它会产生的JavaScript
And here's the JavaScript it would produce
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
注意TypeScript如何定义成员变量和类方法参数的类型。这在转换为JavaScript时被删除,但由IDE和编译器用于发现错误,例如将数字类型传递给构造函数。
Notice how the TypeScript defines the type of member variables and class method parameters. 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.
它还能够推断类型没有显式声明,例如,它会确定 greet()
方法返回一个字符串。
It's also capable of inferring types which aren't explicitly declared, for example, it would determine the greet()
method returns a string.
许多浏览器和IDE通过源图提供直接调试支持。有关详细信息,请参阅此Stack Overflow问题:
Many browsers and IDEs offer direct debugging support through sourcemaps. See this Stack Overflow question for more details: Debugging TypeScript code with Visual Studio
我最初写的这个答案当Typescript仍然热卖时。有关更多最新详细信息,请查看。
I originally wrote this answer when Typescript was still hot-off-the-presses. Check out Lodewijk's answer to this question for some more current detail.
这篇关于什么是TypeScript,为什么我会用它代替JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!