推广 热搜: page  音视频  使用  个数  搜索引擎  选择  企业  可以  父亲  百度 

是时候给TypeScript再一次机会了

   日期:2025-01-02     作者:6omk7    caijiyuan   评论:0    移动:http://ww.kub2b.com/mobile/news/19175.html
核心提示:by Jason Dreyzehner 杰森·德雷泽纳(Jason Dreyzehner) Since 2012, TypeScript has been a popular choice for programmers co

by Jason Dreyzehner

杰森·德雷泽纳(Jason Dreyzehner)

Since 2012, Typescript has been a popular choice for programmers coming to Javascript from more structured languages (like C++ or Java). But it’s also been largely dismissed by those native to the Javascript world.

自2012年以来,Typescript一直是程序员从结构化语言(如C ++或Java)开始使用Javascript的流行选择。 但是,Javascript世界中的本地人也基本上不屑一顾。

You may have heard that the Angular team recently switched to Typescript for Angular 2. So have the teams behind RxJS, Ionic, Cycle.js, Blueprint, Dojo, Nativescript, Plottable, and others.

您可能已经听说Angular团队最近切换到Typescript for Angular 2了 。 RxJS , Ionic , Cycle.js , Blueprint , Dojo , Nativescript , Plottable等等的团队也是如此。

If you’ve been in Javascript/Node.js land for a while, it’s easy to assume that the shot-callers for these projects have lost their minds. Or maybe that they were paid off by Microsoft. ?

如果您已经在Javascript / Node.js领域工作了一段时间,那么很容易就可以认为这些项目的主要召唤者已经失去了理智。 也许它们是由Microsoft付清的。

And if you haven’t been watching closely, you may have missed Typescript’s amazing progress over the past year (and even the past few months).

而且,如果您没有仔细观察,您可能会错过Typescript在过去一年(甚至过去几个月)中取得的惊人进步。

If you’re still thinking “Typescript is kinda like Coffeescript, right?”—this article is for you.

如果您仍在思考“ Typescript有点像Coffeescript,对吗?” —本文适合您。

There are dozens of great resources and articles on the benefits of using Typescript. I hope that after reading this, you’ll take another look.

关于使用Typescript的好处,有很多很棒的资源和文章。 希望阅读完此后再看一遍。

Javascript —是否具有类型(Javascript — with Types?)

For those new to this discussion, it’s important to understand the aversion that much of the Javascript world has to types. Besides its portability, much of Javascript’s popularity could be attributed to its simplicity.

对于本次讨论的新手来说,重要的是要了解许多Javascript世界对类型的厌恶。 除了可移植性之外,Javascript的广泛流行还可以归因于其简单性。

“To be attractive to hackers, a language must be good for writing the kinds of programs they want to write. And that means, perhaps surprisingly, that it has to be good for writing throwaway programs.” — Paul Graham, Being Popular

为了吸引黑客,一种语言必须能够很好地编写他们想要编写的程序。 也许这令人惊讶,这意味着它必须适合编写一次性程序。” 保罗·格雷厄姆(Paul Graham), 受欢迎

The kind of programmers who make Javascript their tool of choice often do so for its flexibility. There’s no standard library, very little structure, and without types, Javascript users don’t need to spend much time thinking about details when hacking on a new idea.

选择Javascript作为其选择工具的那种程序员通常是出于灵活性的考虑。 没有标准的库,很少的结构,而且没有类型,Javascript用户在攻克新主意时不需要花太多时间思考细节。

This is probably easiest to contrast to a language like C++, where programs tend to require a lot more structure and overhead. A lot of Javascript programmers (particularly the above hacker-types) find the tedium of traditional classes, boilerplate, types, and typecasting slow them down.

与C ++这样的语言相比,这可能是最容易的,因为C ++语言的程序往往需要更多的结构和开销。 许多Javascript程序员(尤其是上述黑客类型)发现传统类,样板,类型和类型转换的乏味使它们变慢。

“Give me your tired, your poor, your huddled masses yearning to breathe free — of over-protective programming languages.” — Javascript ?️ (basically)

“请给我您那些疲惫,贫穷,拥挤的群众,让他们呼吸自由的过度保护的编程语言。” - Javascript的 ️(基本上)

With this perspective, it’s easy to see why a lot of Javascript users are so averse to the idea of Javascript with types.

从这个角度来看,很容易看出为什么很多Javascript用户如此反对带有类型Javascript的想法。

Here are some insights that might help to ease those fears.

这里有一些见解可以帮助缓解这些担忧。

Typescript是具有更好棉绒Javascript (Typescript is Javascript with better linting)

Probably one of the most common concerns with the idea of using Typescript is that it isn’t pure Javascript. Because Typescript is its own language, it’s assumed your code will be transpiled into a messy glob which you’ll someday be forced to debug.

关于使用Typescript的想法,最常见的问题之一可能是它不是 Javascript。 由于Typescript是其自身的语言,因此假定您的代码将被转换为一个混乱的glob,有一天您将不得不对其进行调试。

Besides Typescript being extremely well-tested and widely in use, it’s worth noting that depending on your configuration, very little “transpiling” is actually happening (if any). Typescript is just Javascript with optional typings.

除了对Typescript进行了充分的测试和广泛使用以外,值得注意的是,根据您的配置,实际上很少发生“转换”(如果有)。 Typescript只是具有可选类型的Javascript。

Typescript is like a highly-advanced linter, able to check documentation and warn when code is not being used as intended.

Typescript就像一个高级的linter,能够检查文档并在未按预期使用代码时发出警告。

It provides immediate feedback and a better development experience for all future users of your code. This is also a good test for new projects—if your project is worth linting to enforce code style conventions, your project is probably long-lasting enough to benefit from Typescript.

它为您的代码的所有未来用户提供即时反馈和更好的开发体验。 对于新项目来说,这也是一个很好的测试-如果您的项目值得执行代码样式约定,那么您的项目可能足够持久,可以从Typescript中受益。

The Typescript team has committed to tracking Javascript for the foreseeable future. So if/when additional features stabilize in Javascript, Typescript will match them and adapt.

Typescript团队致力于在可预见的将来跟踪Javascript 。 因此,如果/当其他功能稳定在Javascript中时,Typescript将对其进行匹配和调整。

Typescript消除了运行时开销 (Typescript eliminates runtime overhead)

Another common misconception is that Typescript’s type checking somehow persists into the runtime environment, adding complexity and overhead.

另一个常见的误解是Typescript的类型检查以某种方式持续到运行时环境中,从而增加了复杂性和开销。

In fact, Typescript is a good way to avoid runtime type checking overhead.

实际上,Typescript是避免运行时类型检查开销的好方法。

Typescript is a development-time/compile-time tool — it takes in standard Javascript with optional type-hints and outputs Javascript with those hints removed. (If enabled, it can also transpile ES6 and ES7 Javascript features back to current standards.)

Typescript是一个开发时/编译时工具 -它使用带有可选类型提示的标准Javascript,并输出除去了这些提示Javascript。 (如果启用,它还可以将ES6和ES7 Javascript功能转换回当前标准。)

Typescript’s type-hints give us all the benefits of types, and then they disappear.

Typescript的类型提示为我们提供了类型的所有好处,然后它们消失了。

The only clues left at runtime of an object’s type are the same clues provided by standard Javascript features. (For example, when you create a new object from a prototype, you might check its type with .)

在对象类型的运行时剩下的唯一线索与标准Javascript功能提供的线索相同。 (例如,当您从原型创建新对象时,可以使用检查其类型。)

Ironically, because Javascript doesn’t provide a standard means of development-time type checking, many of the most developed Javascript libraries reimplement their own runtime type checking systems.

具有讽刺意味的是,由于Javascript没有提供开发时类型检查的标准方法,因此许多最发达Javascript库都重新实现了自己的运行时类型检查系统

These libraries don’t intend to do this at the outset, but part of providing a good development experience is ensuring developers see clear and actionable errors when they’ve made a mistake.

这些库并非一开始就打算这样做,但是提供良好的开发经验的一部分是确保开发人员在犯错时看到清楚且可操作的错误。

In pursuit of this goal, many libraries extensively check the types of parameters passed to methods at runtime, throwing errors meant only for the eyes of the developer implementing the method.

为了实现这个目标,许多库在运行时广泛检查传递给方法的参数类型,并抛出错误,仅对实现该方法的开发人员而言是错误的。

This is most certainly the worst of both worlds. These cascades of runtime type checks add significant code bloat, make code less readable, and increase the difficulty of maintaining 100% unit test coverage.

这无疑是两全其美 这些级联的运行时类型检查增加了代码繁重的工作量,使代码的可读性降低,并增加了维护100%单元测试覆盖率的难度。

Across large codebases, these runtime tests really add up. After a bit of refactoring, many largest codebases end up with whole type systems.

在大型代码库中,这些运行时测试的确加起来了。 经过一些重构,许多最大的代码库最终都使用了整个类型的系统

Without using Typescript, not only do you lose out on development-time type checking—you often shift it into runtime. (I hope you have full test coverage.)

如果不使用Typescript,不仅会浪费开发时进行的类型检查,还会经常将其转移到运行时。 ( 我希望您有完整的测试范围。 )

When you use Typescript, you provide your users with an even better development experience, reduce runtime type checking to only cases where it’s needed (sanitizing end-user input, for example), and make your code easier to fully unit test.

使用Typescript时,可以为用户提供更好的开发体验,将运行时类型检查减少到仅在需要时才进行检查(例如,对最终用户输入进行消毒),并使代码更容易进行完整的单元测试。

Typescript已经走了很长一段路 (Typescript has come a long way)

Maybe for the reasons mentioned above, when I first heard of Typescript, I ran the opposite direction as fast as I could. Besides being antithetical to the “best thing about Javascript” (less structure), it was made by Microsoft.

也许由于上述原因,当我第一次听说Typescript时,我会尽可能快地朝相反的方向走。 除了与“关于Javascript的最好的东西”(较少的结构)相反,它是由Microsoft制造的

But it’s not 2012 anymore. Typescript is not a leaky abstraction of Javascript, and the Typescript project has some of the best hackers and engineers in this space. (And I’m impressed with how well Microsoft is managing it.)

但这不是2012年。 Typescript不是Javascript的泄漏抽象 ,并且Typescript项目拥有该领域的一些最佳黑客和工程师。 (我对Microsoft的管理水平印象深刻。)

Since Typescript tracks ECMAscript, using Typescript doesn’t lock your project to a new language. A lot of people still don’t realize this, so it’s not uncommon to hear sentiments like:

由于Typescript跟踪ECMAscript,因此使用Typescript不会将您的项目锁定为新的语言。 许多人仍然没有意识到这一点,因此听到诸如以下这样的观点并不少见

“It’s hard to maintain a Typescript project.”
“很难维护Typescript项目。”

Which, to me, sounds like:

在我看来,这听起来像

“It’s hard to maintain a project with linting.”
“很难通过棉绒来维持项目。”

If your project somehow stops benefitting from Typescript, you can run your project through the compiler (one last time) to remove all types from your codebase.

如果您的项目因某种原因停止从Typescript中受益,则可以通过编译器运行项目(最后一次),以从代码库中删除所有类型。

Then you’re back to untyped Javascript.

然后,您将返回未键入Javascript。

TL; DR (TL;DR)

Typescript has improved a lot recently. If you heard about Typescript years ago, but haven’t really followed it since then, it’s worth another look.

Typescript最近有了很大的改进。 如果您是几年前听说Typescript的,但是从那以后还没有真正关注它,那么值得再看看。

何时使用Typescript (When to use Typescript)

Angular:为什么选择Typescript (Angular: Why Typescript?)

A short technical discussion of exactly why the Angular team chose Typescript to build Angular 2.

关于Angular团队为何选择Typescript来构建Angular 2的简短技术讨论。

所有JS库均应使用Typescript编写 (All JS Libraries Should be Authored in Typescript)

A summary of why Typescript is a good idea for JS libraries, from the creator of Cycle.js and contributor to RxJS.

从Cycle.js的创建者到RxJS的贡献者,总结了为什么Typescript是JS库的一个好主意。

Typescript深入研究-为什么选择Typescript (Typescript Deep Dive — Why Typescript)

A good summary of the benefits of using Typescript. (Typescript Deep Dive is a great general reference.)

关于使用Typescript的好处的很好的总结。 ( Typescript Deep Dive是一个很好的通用参考。)

了解Typescript (Learn about Typescript)

Typescript教程 (The Typescript tutorial)

A short tutorial maintained by the Typescript team.

Typescript团队维护的简短教程。

Typescript设计目标 (Typescript Design Goals)

A short wiki outlining the Typescript team’s general design principles.

简短的Wiki,概述了Typescript团队的一般设计原则。

打字稿启动器 (typescript-starter)

A boilerplate project for building Javascript libraries. Includes proper unit testing, documentation generation, and both CommonJS and ES6 Module exports (for Node.js and the browser).

用于构建Javascript库的样板项目。 包括适当的单元测试,文档生成以及CommonJS和ES6模块导出(针对Node.js和浏览器)。

I wrote this with the hope of changing minds. If you have any ideas for how I could improve this article, please let me know.

我写这篇文章是为了改变主意。 如果您对如何改进本文有任何想法,请告诉我 。

Please ♡ and share this post if you found it interesting. Thanks for reading!

如果您发现它有趣,请♡并分享此帖子 。 谢谢阅读

本文地址:http://ww.kub2b.com/news/19175.html     企库往 http://ww.kub2b.com/ ,  查看更多

特别提示:本信息由相关用户自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。

 
 
更多>同类最新文章
0相关评论

文章列表
相关文章
最新动态
推荐图文
最新文章
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2020018471号