typescript是什么
TypeScript is a compile-to-JavaScript language that brings compile-time type checks, classical object-oriented programming patterns, and powerful typing features to JavaScript.
TypeScript是一种JavaScript编译语言,它为JavaScript带来了编译时类型检查,经典的面向对象的编程模式以及强大的键入功能。
![](https://scotch-res.cloudinary.com/image/upload/w_auto,q_auto:good,f_auto/media/1/X8GCDwpRhuWpsly2OVc5_9dgJxPr.png)
Unsurprisingly, quite a few people see TypeScript as suspicious, at best. And that's fair enough -- at the end of the day, even TypeScript compiles down to raw JS. It's not like we absolutely need an intermediate step. Being a purist myself, I understand the sentiment!
毫不奇怪,很多人充其量只是认为TypeScript可疑。 这很公平-归根结底,即使TypeScript也可以编译为原始JS。 并不是说我们绝对需要一个中间步骤。 我本人是一个纯粹主义者,所以我理解这种情绪!
But, there are real benefits to the extras that TypeScript brings, whether you use it or not; and there's no reason for suspicion, either way.
但是,不管是否使用TypeScript,TypeScript所带来的额外功能都有真正的好处。 两种方式都没有理由怀疑。
为什么不只是JavaScript? ( Why Not Just JavaScript? )
The litany of languages that compile to JavaScript seems to grow by the day: CoffeeScript, TypeScript, ClojureScript, GorillaScript ... With all the options, the question arises as to why. Why bother with all the complexity of a new language and a new toolchain, if all we get in the end is just JavaScript? Why not Vanilla JS?
如今 ,可编译为JavaScript的语言的种类越来越多了: CoffeeScript , TypeScript , ClojureScript , GorillaScript ...有了所有的选择,就产生了为什么的问题。 如果最终我们仅是JavaScript ,为什么还要烦恼新语言和新工具链的所有复杂性? 为什么不使用Vanilla JS ?
There are a number of reasons people build transpiled languages, but at the top of the list are:
人们构建转译语言的原因有很多,但列表的顶部是:
- Additional features; and 附加功能; 和
- Freedom to innovate. 自由创新。
转译语言添加功能 (Transpiled Languages Add Features)
One substantial advantage to transpiled languages is that they can * add features **. This is one of TypeScript's main selling points, as it adds interfaces, abstract classes, algebraic data types, and other features to JavaScript, which we *wouldn't be able to use otherwise.
转译语言的一大优势是它们可以*添加功能**。 这是TypeScript的主要卖点之一,因为它向JavaScript添加了接口,抽象类,代数数据类型和其他功能,否则我们将无法使用它们 。
The transpiled language implements a feature in raw JavaScript, and lets you work with the interface without having to worry about the details. Truth is, we do this even when we write normal JavaScript: We still use ES6-to-ES5 transpilers to write to use JavaScript's newest features, since the relatively small number of people using browsers with reliable ES6 support is . . . Well, people like us.
所编译的语言在原始JavaScript中实现了一项功能,使您可以使用界面而不必担心细节。 事实是,即使编写普通JavaScript,我们也会这样做:我们仍然使用ES6-to-ES5编译器来编写以使用JavaScript的最新功能,因为使用支持可靠ES6的浏览器的人数相对较少。 。 。 好吧,像我们这样的人。
翻译语言可以自由创新 (Transpiled Languages Are Free to Innovate)
CoffeeScript is a good example of a language that innovated atop its JavaScript base. This is immediately obvious syntactically:
CoffeeScript是在其JavaScript基础之上进行了创新的一种很好的语言示例。 这在语法上立即显而易见:
# Ruby-style comments w/ hash, no semicolons, only arrow functions
Person = (name) =>
# @ indicates 'this'
@name = name
@speak = () =>
console.log @name
return @
Me = Person('Peleke')
Me.speak()
It doesn't even look like JavaScript. New syntax includes:
它甚至看起来都不像JavaScript。 新语法包括:
@
indicatesthis
, sothis.user
becomes@user
; and@
表示this
,因此this.user
变为@user
; 和- Functions are defined with
=>
or->
, not the function keyword 函数是使用=>
或->
而不是 function关键字定义的
. . . And CoffeeScript doesn't allow ==
, either: It forces ===
, unless you really want type coercion and embed JavaScript between backticks.
。 。 。 而且CoffeeScript也不允许==
:它强制===
,除非您真的想要键入强制并将JavaScript插入反引号之间。
TypeScript has completely different goals, and is much more conservative than this. Rather than experiment with new syntax and opinionated design decisions, it takes JavaScript as its starting point, and adds to it * while departing minimally from the standard **. With *very few exceptions, vanilla ES6 is perfectly valid TypeScript. That makes it much more accessible than other compile-to-JS languages.
TypeScript具有完全不同的目标,并且比这要保守得多。 与其尝试新的语法和过硬的设计决策,不如将JavaScript作为起点,并添加到*中,同时尽量不偏离标准**。 除了极少数例外,香草ES6是完全有效的TypeScript。 这使得它比其他的JS编译语言更易于访问。
A transpiled language is a more radical idea than new JavaScript transpiled to old JavaScript, but the principle is similar, and the value of transpiled languages is well-established: ** They give us the power to experiment, expand, and express ourselves in new ways **.
与新JavaScript移植到旧JavaScript相比,翻译的语言是一个更激进的想法,但是原理相似,并且翻译的语言的价值已得到公认:**它们使我们有能力在新的语言中进行实验,扩展和表达自己方法 **。
TypeScript的优势 ( Strengths of TypeScript )
TypeScript brings several new features to the table. The most notable of these are classes, interfaces, and compile-time type checks, but there's quite a bit more than that lurking in the depths.
TypeScript为表带来了几个新功能。 其中最著名的是class , interfaces和编译时类型检查 ,但潜伏于深度的还远远不止这些。
I'll talk about a few of these, but would like to call out a few of TypeScript's less-discussed strengths:
我将讨论其中的一些优点,但想讲一下TypeScript较少讨论的优点:
- Familiarity; 熟悉;
- Support & Toolchain Improvements; and 支持和工具链改进; 和
- Near-Total JavaScript Interop. 几乎全部JavaScript互操作。
熟识 (Familiarity)
There's a lot of talk about TypeScript's features. There's a lot less about why they're there.
关于TypeScript功能的讨论很多。 他们为什么会在那里的信息要少得多。
That's largely because TypeScript's most famous features -- compile-time type checks, interfaces, class-based design patterns -- are so familiar that many developers from other backgrounds take them for granted. The fact that JavaScript lacks these features is a common pain point for developers coming from other popular curly-brace languages, and TypeScript adds them because classical OOP is the most familiar way for them to design, develop, and discuss software.
这主要是因为TypeScript最著名的功能-编译时类型检查,接口,基于类的设计模式-如此熟悉,以至于许多其他背景的开发人员都将其视为理所当然。 JavaScript缺少这些功能的事实是使用其他流行的花括号语言的开发人员的共同难题,而TypeScript则添加了这些功能,因为经典的OOP是他们设计,开发和讨论软件的最熟悉的方式。
You could argue that this is no excuse not to just learn the language you're writing in rather than the one you're used to. It would be a legitimate argument: Asking, say, the Haskell community why you can't "just do IO" won't go over as well as whining about JavaScript's lack of whatever-it-is does amongst web developers.
您可能会争辩说,这不是仅学习所用语言而不是惯用的语言的借口。 这将是一个合理的论点:比如说,问问Haskell社区为什么您不能“仅仅做IO”就不会像对JavaScript缺乏Web开发人员所做的任何事情那样抱怨。
Regardless, the fact remains that people are more productive when they're comfortable with their tools
无论如何,事实仍然是人们在对工具感到满意时会提高工作效率
If TypeScript makes developers more productive, they can -- and should -- use it. Forcing them through a largely idiolectic learning curve just to get things done is impractical in real projects.
如果TypeScript使开发人员提高生产力,他们可以并且应该使用它。 在实际的项目中,强迫他们通过主要的学习曲线来完成任务是不切实际的。
One semi-legitimate criticism of TypeScript is that it creates the illusion that JavaScript has features that it actually doesn't. The culprits are the usual suspects: Classes, interfaces, and static typing are not features we can expect any time soon in JavaScript, and having them in TypeScript is not the same thing.
对TypeScript的半合法性批评是,它造成了一种幻觉,即JavaScript具有实际上没有的功能。 罪魁祸首是通常的疑惑:类,接口和静态类型不是我们很快就会在JavaScript中期望的功能,而将它们包含在TypeScript中并不是同一回事。
Writing TypeScript with the misconception that it implements "true" classes, interfaces, and runtime type checking is guaranteed to lead to awkward code. The fact that TypeScript adds sugar isn't license to ignore its JavaScript underbelly. But, for those who do know their JavaScript well, and think better with TypeScript's extras, using TypeScript can make them better at what they do.
写作打字稿与它实现了误解,“真正的”类,接口和运行时类型检查是保证导致尴尬的代码。 TypeScript添加糖的事实并不意味着可以忽略其JavaScript。 但是,对于那些确实精通JavaScript并且对TypeScript的附加功能有更好思考的人,使用TypeScript可以使他们的工作做得更好。
支持 (Support)
TypeScript also has the advantage of having some giants behind it.
TypeScript还具有一些强大的优势。
Whereas most compile-to-JS languages are community efforts, TypeScript is an official Microsoft endeavor. They've put a lot of time into developing it, and users can rest assured that they'll have long-term support from the tech giant should they migrate to TypeScript.
尽管大多数可编译为JS的语言都是社区的努力,但TypeScript是Microsoft的一项正式举措。 他们投入了大量时间进行开发,用户可以放心,如果他们迁移到TypeScript,他们将获得技术巨头的长期支持。
TypeScript has support for all the most popular editors including Sublime Text, Atom, Eclipse, Emacs, WebStorm, Vim, and of course Microsoft's Visual Studio family.
TypeScript支持所有最受欢迎的编辑器,包括Sublime Text,Atom,Eclipse,Emacs,WebStorm,Vim,当然还有Microsoft的Visual Studio系列。
![](https://i-blog.csdnimg.cn/blog_migrate/2d13621831ffea848827ef556959ca72.png)
It's used in a number of real-world projects, too, so you can trust it's robust enough to handle just about anything you're working on:
它也用于许多实际项目中,因此您可以相信它足够强大,可以处理您正在处理的任何事情:
Black Screen is an OS X terminal emulator & IDE.
黑屏是OS X终端仿真器和IDE。
Angular 2's main codebase is written in TypeScript, and a substantial proportion of the community is sure to jump on board with it.
Angular 2的主要代码库是用TypeScript编写的,并且相当一部分社区肯定会加入其中。
Microsoft's Salsa service, which powers part of Visual Studio, also uses TS.
Microsoft的Salsa服务 (支持Visual Studio的一部分)也使用TS。
Take a look at the list of TypeScript users.
看一下TypeScript用户列表。
Whether or not you use Angular 2 or Visual Studio, that kind of endorsement from both Microsoft and one of Google's most prominent web teams should help you sleep at night if you're about support and longevity.
无论您使用的是Angular 2还是Visual Studio,如果您要获得支持和延长寿命,那么Microsoft和Google最著名的网络团队之一的这种认可应该可以帮助您在晚上入睡。
TypeScript is also a major step forward for those who use IDEs. According to Hejlsberg, who's at the head of the TypeScript project, JavaScript's lack of type annotations makes it practically impossible to build a robust IDE. For the hacker-types whose custom is one-(wo)manning NPM modules in Vim, that's a low priority. But a powerful development environment can make teams collaborating on large projects substantially more productive.
对于使用IDE的人来说,TypeScript也是重要的一步。 根据TypeScript项目负责人Hejlsberg的说法,JavaScript缺乏类型注释,因此实际上不可能构建健壮的IDE。 对于自定义为Vim中一个(wo)人员NPM模块的黑客类型,这是低优先级的。 但是强大的开发环境可以使在大型项目上进行协作的团队效率更高。
几乎全部JavaScript互操作 (Near-Total JavaScript Interop)
As a bit of a purist, I'd have that TypeScript's most important benefit is that it's just JavaScript. Running raw JavaScript through the TypeScript compiler will rarely cause trouble (the major exception being with class declarations), so migrating to TypeScript can be as gradual as you want.
作为一个纯粹主义者,我想知道TypeScript的最重要的好处就是它只是JavaScript 。 通过TypeScript编译器运行原始JavaScript几乎不会造成麻烦( 主要的例外是类声明 ),因此,可以根据需要逐步迁移到TypeScript。
That means you can use the features that help you think, and forget about the ones that don't. When I write TypeScript, I don't use classes, mixins, generics, or enums, but I do use type annotations, aliases, and ADTs quite a bit.
这意味着您可以使用可帮助您思考的功能,而忽略那些功能则不然。 当我编写TypeScript时,我没有使用类,mixin,泛型或枚举,但是我确实使用了类型注释,别名和ADT。
You might choose to use exactly the tools I don't, or toss in a type annotation only every once in awhile. That's fine with TypeScript: You can use as much, or as little, as you want. It is not an all-or-nothing commitment: It's only as total as you make it.
您可能会选择完全使用我不使用的工具,或者每隔一段时间仅使用一次类型注释。 使用TypeScript很好:您可以根据需要使用任意数量的元素。 这不是一个全有或全无的承诺:它只是您所做的全部。
You can use as much, or as little, as you want. It is not an all-or-nothing commitment: It's only as total as you make it.
您可以根据需要使用任意数量的内容。 这不是一个全有或全无的承诺:它只是您所做的全部。
TypeScript的历史 ( TypeScript's History )
To quote of core developer Anders Hejlsberg, TypeScript was born in response to complaints from clients and internal teams that JavaScript didn't lend itself well to large applications.
引用核心开发人员安德斯·海尔斯伯格 ,打字稿出生响应来自客户和内部团队投诉JavaScript并不太适合于大型应用程序。
The goal was to "strengthen JavaScript with things like classes, modules, and static typing", without sacrificing the advantage of its being open-standards and cross-platform; the result was a "language for application scale javascript development", built as a superset of the language.
其目标是“在不牺牲其开放标准和跨平台优势的情况下,通过类,模块和静态类型之类的东西来增强JavaScript”。 结果是作为该语言的超集构建的“应用程序级javascript开发语言”。
TypeScript与* Scripts ( TypeScript vs *Scripts )
Even if TypeScript does bring a lot to the table, the question as to why we need another compile-to-JS language remains a fair one.
即使TypeScript 确实带来了很多好处,但关于为什么我们需要另一种可编译为JS语言的问题仍然是一个公平的问题。
Different projects pose different engineering challenges, and different languages provide different sets of solutions and tradeoffs. Different compile-to-JS languages take different approaches to solving the problem of simplifying, streamlining, and strengthening the engineering process.
不同的项目带来不同的工程挑战,不同的语言提供不同的解决方案和折衷方案。 不同的JS编译语言采用不同的方法来解决简化,精简和增强工程流程的问题。
That means they end up solving different problems in dfferent ways, and are particularly suited to specific tasks. CoffeeScript lends itself well to configuration files and beautiful tests, neither of which I'd ever prefer to write with TypeScript.
这意味着他们最终将以不同的方式解决不同的问题,并且特别适合于特定任务。 CoffeeScript的很适合配置文件和美丽的测试 ,均未我会永远喜欢用打字稿写。
Two common points of comparison are Dart and CoffeeScript. To be frank, the most these three languags have in common is that they compile to JavaScript: It really is an apples-to-kumquats comparison. Taking a closer look at some of the differences in their design decisions should clarify why.
比较的两个常见点是Dart和CoffeeScript。 坦率地说,这三种语言的共同点是它们可以使用JavaScript进行编译:这确实是苹果与金橘的比较。 仔细研究他们的设计决策中的一些差异,可以阐明原因。
CoffeeScript (CoffeeScript)
![](https://scotch-res.cloudinary.com/image/upload/w_auto,q_auto:good,f_auto/media/1/BhemLuLT4uGqTOjcOowA_mXBwbxa.png)
Anders Hejlsberg identifies two fundamental differences between TypeScript and CoffeeScript:
Anders Hejlsberg指出了TypeScript和CoffeeScript之间的两个基本区别:
- * CoffeeScript is a different language **. TypeScript is a *superset of JavaScript. CoffeeScript targets the JavaScript platform, but is otherwise a different language. * CoffeeScript是另一种语言**。 TypeScript是 JavaScript的超集 。 CoffeeScript以JavaScript平台为目标,但使用另一种语言。
- ** CoffeeScript does not feature static typing **. This is mostly relevant to folks who use IDEs (some of us don't). ** CoffeeScript不具有静态类型**。 这与使用IDE的人最相关( 我们中有些人不使用 )。
To quote Hejlsberg further, "if you know JavaScript, you already know TypeScript". You've got to pick up a handful of extras, but the that should take all of a day for those familiar with raw JavaScript and ES6. CoffeeScript, on other hand, requires you to learn a completely different syntax. It's accessible, but the learning curve is steep in comparison.
进一步引用Hejlsberg的话,“如果您知道JavaScript,您已经知道TypeScript”。 您必须挑选一些额外的功能,但是对于那些熟悉原始JavaScript和ES6的人来说,那应该花一整天的时间。 另一方面,CoffeeScript要求您学习完全不同的语法 。 可以访问,但是相比之下,学习曲线陡峭。
Maintaining such overlap with the target language was a fundamental tenet of the TypeScript's design. The whole point was to create a language that ** simplifies the creation of large JavaScript applications *, while * departing minimally from the standard **.
与目标语言保持这种重叠是TypeScript设计的基本原则。 重点是要创建一种简化大型JavaScript应用程序*的语言,而*与标准**的差异最小。
CoffeeScript made no attempt to be a superset of JavaScript. Instead, it sought to "expose the language's good parts", while suppressing its infamous "bad parts" and enhancing expressivity.
CoffeeScript没有尝试成为JavaScript的超集。 相反,它试图“公开语言的主要部分”,同时压制臭名昭著的“坏部分”并增强表达能力。
The most obvious difference between the two approaches are that:
两种方法之间最明显的区别是:
- TypeScript is ** largely unopinionated *k but seeks to be * better equipped to scale **, whereas TypeScript **在很大程度上不受限制* k,但是旨在*能够更好地进行缩放**,而
- CoffeeScript ** builds its opinions into the language **, and adds no extras explicitly intended to ease the creation of large applications. CoffeeScript **将其意见嵌入到**语言中,并且未添加任何明确旨在简化大型应用程序创建的附加功能。
TypeScript is obviously of the opinion that class-oriented programming and static typing are useful tools, but makes no attempt to coerce you into using them. CoffeeScript, by contrast, has strong opinions as to what is good or bad abou JavaScript, and exposes functionality accordingly.
TypeScript显然认为面向类的编程和静态类型化是有用的工具,但是并没有试图强迫您使用它们。 相比之下,CoffeeScript对JavaScript的优缺点有很强的见解,并相应地公开了功能。
That's not to suggest one is better than the other -- Brendan Eich has mentioned CoffeeScript's influence on JavaScript multiple times. The point is rather that they're different beasts, with different goals, and different solutions.
这并不是说一个比另一个要好-Brendan Eich多次提到CoffeeScript对JavaScript的影响。 关键是它们是不同的野兽,具有不同的目标和解决方案。
镖 (Dart)
![](https://scotch-res.cloudinary.com/image/upload/w_auto,q_auto:good,f_auto/media/1/2Fb7qQkcQxuhzPsiriGV_h57QDgx.png)
Dart is syntactically more similar to TypeScript than CoffeeScript, but departs substantially from both languages with regard to its design philosophy.
Dart在语法上与TypeScript相比比CoffeeScript更相似,但就其设计理念而言,它与两种语言都大相径庭。
Dart and CoffeeScript agree that JavaScript needs fixing, but solve the problem differently. The Dart team identifies two substantial differences with CoffeeScript:
Dart和CoffeeScript同意JavaScript需要修复,但是以不同的方式解决问题。 Dart团队发现CoffeeScript有两个实质性的区别:
- Dart makes different syntactical choices; and Dart做出了不同的语法选择; 和
- "Dart introduces new semantics, while CoffeeScript retains the semantics of JavaScript." “ Dart引入了新的语义,而CoffeeScript保留了JavaScript的语义。”
Similarly, Dart and TypeScript "have similar goals [of making] building large-scale web applications easier", but take different approaches to achieving that end: "** Dart purposely made a break from certain parts of JavaScript’s syntax and semantics in order to eradicate large classes of bugs and to improve performance **", whereas "TypeScript maintains backwards compatability with JavaScript".
同样,Dart和TypeScript“(使)构建大型Web应用程序的目标相似(但具有类似的目标),但采用了不同的方法来实现这一目标:“ ** Dart故意破坏了JavaScript语法和语义的某些部分,以便消除了大类的错误并提高了性能**”,而“ TypeScript保持了与JavaScript的向后兼容性”。
This is a substantial difference in opinion from that of the TypeScript team, which asked, "what are, qualitatively, the few things that are missing from JavaScript itself, and how can we add those without disturbing all of the advantages of JavaScript", and aligned its additions against the answer.
这与TypeScript团队的观点有很大的不同。TypeScript团队问:“从质上讲,JavaScript本身缺少的几样东西,以及我们如何在不干扰JavaScript的所有优点的情况下添加这些东西”,以及使添加的内容与答案保持一致。
"What are, qualitatively, the few things that are missing from JavaScript itself, and how can we add those without disturbing all of the advantages of JavaScript?" ~ Hejlsberg
“从本质上说,JavaScript本身缺少的是几样东西,我们如何在不干扰JavaScript所有优点的情况下添加这些东西?” 〜海斯伯格
The Dart team decided there are more than "a few things missing from JavaScript", and designed the language accordingly. And, where TypeScript is concerned exclusively with improving the development experience on the web, Dart is built with mobile app development, as well. That's a whole new can of worms to consider as a language designer and, while it's a fantastic language, it's also a very different one.
Dart团队认为,“ JavaScript还缺少一些东西”,并据此设计了该语言。 而且,在TypeScript专门致力于改善Web开发体验的情况下,Dart也使用移动应用程序开发来构建。 作为一种语言设计者,这是一种全新的蠕虫病毒,尽管它是一种很棒的语言,但它也是一种截然不同的语言。
结论 ( Conclusion )
JavaScript has done its job just fine for 20 years. At the end of the day, it's hard to argue that we need extras. On the other hand, it's hard to argue that we can't benefit from them. TypeScript:
JavaScript已经干了20年了。 归根结底,很难说我们需要额外的东西。 另一方面,很难说我们不能从中受益。 打字稿:
- Is more comfortable for a lot of people than raw JavaScript; 对于许多人来说,比原始JavaScript更舒适;
- Is flexible with what you leave and what you take; 对您的离开和携带的物品具有灵活性;
- Enables more powerful tooling; and 启用更强大的工具; 和
- At the end of the day, can still ** just be JavaScript **. 归根结底,仍然可以仅是JavaScript **。
For my part, I use TypeScript whenever I work with Angular 2 or Rx.js, and when writing heavily functional code (I've got a habit of typing functions). The rest of the time, I stick to vanilla JS. Switching between the two is simple enough I don't think about it anymore.
就我而言,每当我与Angular 2或Rx.js一起使用时,以及在编写大量功能代码时(我都有键入函数的习惯),我都会使用TypeScript。 其余时间,我坚持使用香草JS。 两者之间的切换非常简单,我不再考虑了。
"JavaScript has some quirks, but you can teach a compiler about the quirks and you can have it check all of these things." ~ Anders Hejlsberg
“ JavaScript有一些怪癖,但是您可以教编译器有关怪癖的知识,并且可以让它检查所有这些问题。” 〜 安德斯·海斯伯格
Whether you use it or not, TypeScript has some indisputable strengths, and experimenting with it is instructive. If you're open to trying something new, give the documentation a glance, write something small, and see how you feel. If you love it, now you know. If you hate it, that's fine too -- just change the extension back to .js
and get back to it!
不管您是否使用它,TypeScript都有一些无可争辩的优势,尝试使用它具有启发性。 如果您愿意尝试新事物,请看一下文档 ,写一些小东西,然后看看您的感受。 如果您喜欢它,现在就知道了。 如果您讨厌它,那也很好-只需将扩展名更改回.js
即可!
How do you feel about TypeScript? Love it? Hate it? No opinion? Whatever they are, leave your thoughts in the comments, or shoot them to me on Twitter (@PelekeS)!
您对TypeScript的感觉如何? 爱它? 讨厌它? 没有意见吗? 无论它们是什么,都可以在评论中留下您的想法,或者在Twitter( @PelekeS ) 上将它们发给我!
翻译自: https://scotch.io/tutorials/why-you-shouldnt-be-scared-of-typescript
typescript是什么