Javascript Babel 和 TypeScript 的主要区别是什么

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

What are the main differences between Babel and TypeScript

javascripttypescriptbabeljs

提问by DevWL

I know that TypeScript was used to write Angular2, which probably makes it a better choice for someone who wants to get into Angular2, but when I look at Babel it looks very much like TypeScript.

我知道 TypeScript 被用来编写 Angular2,这对于想要进入 Angular2 的人来说可能是一个更好的选择,但是当我看到 Babel 时,它看起来非常像 TypeScript。

I noticed that many well known companies stick to Babel.

我注意到许多知名公司都坚持使用 Babel。

Some questions:

一些问题:

  1. What advantages do they have over each other?
  2. Which make them better or worse choice for the project/developer?
  3. What are the major differences between them and what make them unique?
  1. 他们彼此相比有什么优势?
  2. 哪个让他们成为项目/开发人员更好或更差的选择?
  3. 它们之间的主要区别是什么,是什么使它们与众不同?

回答by Marty

TypeScriptis a superset of JavaScript that compiles down into plain JavaScript (ES3+). The main goal of TypeScript is to enable developers to leverage excellent static typing capabilities. It is suitable for large applications that would benefit from features like:

TypeScript是 JavaScript 的超集,可编译为纯 JavaScript (ES3+)。TypeScript 的主要目标是使开发人员能够利用出色的静态类型功能。它适用于可受益于以下功能的大型应用程序:

  • Type annotations & type inference.
  • Generics.
  • Interfaces, enums, namespaces, modules and classes (the latter two available in ES6).
  • Safe refactoring.
  • 类型注释和类型推断。
  • 泛型。
  • 接口、枚举、命名空间、模块和类(后两者在 ES6 中可用)。
  • 安全重构。

As far as I am aware, Babelsimply "transpiles" newer ECMAScript features down into a format that is supported by older ECMAScript environments. It is suitable for developers who want to write plain JavaScript using newer language features.

据我所知,Babel只是将较新的 ECMAScript 功能“转换”为旧 ECMAScript 环境支持的格式。它适用于想要使用较新的语言功能编写纯 JavaScript 的开发人员。

回答by Tawfiq abu Halawah

Victor Savkin one of angular 2 developers talks why they selected Typescript over other technologies.

Victor Savkin 是 angular 2 开发人员之一,他谈到了为什么他们选择 Typescript 而不是其他技术。

http://victorsavkin.com/post/123555572351/writing-angular-2-in-typescript

http://victorsavkin.com/post/123555572351/writing-angular-2-in-typescript

the last section Why Typescript?

最后一部分 为什么打字稿?

"There are a lot of options available to frontend devs today: ES5, ES6 (Babel), TypeScript, Dart, PureScript, Elm, etc.. So why TypeScript?

“如今前端开发人员有很多选择:ES5、ES6 (Babel)、TypeScript、Dart、PureScript、Elm 等等。那么为什么是 TypeScript?

Let's start with ES5. ES5 has one significant advantage over TypeScript: it does not require a transpiler. This allows you to keep your build setup simple. You do not need to set up file watchers, transpile code, generate source maps. It just works. For many small projects this simplicity outweighs the advanced refactoring and navigation capabilities TypeScript provides. You just know where all the code is, and what it does.

让我们从 ES5 开始。ES5 与 TypeScript 相比有一个显着优势:它不需要转译器。这使您可以保持构建设置简单。您不需要设置文件观察器、转译代码、生成源映射。它只是有效。对于许多小型项目,这种简单性超过了 TypeScript 提供的高级重构和导航功能。您只知道所有代码在哪里,以及它的作用。

ES6 requires a transpiler, so the build setup will not much different from TypeScript. But it is a standard, which means that every single editor and build tool either supports ES6 or will support it.

ES6 需要转译器,因此构建设置与 TypeScript 没有太大区别。但它是一个标准,这意味着每个编辑器和构建工具要么支持 ES6,要么将支持它。

Elm and PureScript are elegant languages with powerful type systems that can prove a lot more about your program than TypeScript can. The code written in Elm and PureScript can be a lot terser than similar code written in ES5.

Elm 和 PureScript 是优雅的语言,具有强大的类型系统,可以比 TypeScript 更能证明您的程序。用 Elm 和 PureScript 编写的代码比用 ES5 编写的类似代码简洁得多。

Each of these options has pros and cons, but I think TypeScript is in a sweet spot that makes it a great choice for most projects. TypeScript takes 95% of the usefulness of a good statically-typed language and brings it to the JavaScript ecosystem. You still feel like you write ES6: you keep using the same standard library, same third-party libraries, same idioms, and many of the same tools (e.g., Chrome dev tools). It gives you a lot without forcing you out of the JavaScript ecosystem."

这些选项中的每一个都有优点和缺点,但我认为 TypeScript 处于最佳位置,使其成为大多数项目的绝佳选择。TypeScript 具有良好的静态类型语言 95% 的有用性,并将其带入 JavaScript 生态系统。您仍然觉得自己在编写 ES6:您继续使用相同的标准库、相同的第三方库、相同的习惯用法和许多相同的工具(例如,Chrome 开发工具)。它为您提供了很多,而不会迫使您退出 JavaScript 生态系统。”