什么是TypeScript?为什么我要用它代替JavaScript? [关闭]

本文翻译自:What is TypeScript and why would I use it in place of JavaScript? [closed]

Closed . 已关闭 This question needs to be more focused . 这个问题需要更加集中 It is not currently accepting answers. 它当前不接受答案。

Want to improve this question? 想改善这个问题吗? Update the question so it focuses on one problem only by editing this post . 更新问题,使其仅通过编辑此帖子来关注一个问题。

Closed 2 years ago . 2年前关闭。

Can you please describe what the TypeScript language is? 您能描述一下TypeScript语言是什么吗?

What can it do that JavaScript or available libraries cannot do, that would give me reason to consider it? JavaScript或可用的库无法执行的工作是什么,这使我有理由考虑?


#1楼

参考:https://stackoom.com/question/rGQU/什么是TypeScript-为什么我要用它代替JavaScript-关闭


#2楼

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对这个问题的答案,以获取更多当前的详细信息。


#3楼

" TypeScript Fundamentals " -- a Pluralsight video-course by Dan Wahlin and John Papa is a really good, presently (March 25, 2016) updated to reflect TypeScript 1.8, introduction to Typescript. TypeScript基础知识 ” - Dan Wahlin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值