什么是 TypeScript,为什么我要用它来代替 JavaScript? [英] What is TypeScript and why would I use it in place of JavaScript?

查看:39
本文介绍了什么是 TypeScript,为什么我要用它来代替 JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能描述一下什么是 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?

推荐答案

我最初在 TypeScript 仍然存在的时候写了这个答案热销新闻.五年后,这是一个不错的概述,但看在Lodewijk 的回答下面获得更多深度

1000 英尺视图...

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

要了解我的意思,请观看 Microsoft 的介绍视频 关于语言.

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,您只能在键入时获得智能智能感知.最初,这只是 Microsoft 的 Visual Studio(也在 Miguel de Icaza).现在,其他 IDE 也提供 TypeScript 支持.

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,但这确实有不同的用途.恕我直言,CoffeeScript 为人类提供了可读性,但 TypeScript 还通过其可选的静态类型为 工具 提供了深度可读性(请参阅此 最近的博文 以获得更多批评).还有 Dart 但这是 JavaScript 的完整替代品(尽管它 可以生成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(你可以在 TypeScript Playground 中玩这个)

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 通过源映射提供直接调试支持.有关更多详细信息,请参阅此堆栈溢出问题:使用 Visual Studio 调试 TypeScript 代码

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 还很火的时候写了这个答案.查看Lodewijk 对这个问题的回答,了解更多最新细节.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆