静态类型 强类型_如何采用静态类型

静态类型 强类型

重点(Top highlight)

静态打字并不适合所有人!(Static Typing is not for everyone!)

“Getting organized is a sign of self-respect.” — Gabby Bernstein

“组织起来是自尊的标志。” —加比·伯恩斯坦(Gabby Bernstein)

The whole idea of programming in JavaScript is to understand the system. One of the key hallmarks of a good programmer is to always know with which types is he working. We’re continuously adjusting a coding style which will make our variables and its types more obvious.

使用JavaScript进行编程的整个想法是理解系统。 优秀程序员的关键标志之一是始终知道他使用哪种类型的软件。 我们正在不断调整编码风格,这将使我们的变量及其类型更加明显。

什么静态类型给我们? (What static typing gives us?)

Up to 2012, we have used JavaScript without any variety of syntactical sugar. In 2012, TypeScript (abbr. TS) was introduced by Microsoft as a new tool which extends JavaScript by adding types to the language. The main goal of TS is to restrict the sort of values that can come in for type comparison.

截至2012年,我们一直在使用JavaScript,而没有使用任何形式的语法糖。 2012年, Microsoft引入了TypeScript (简称TS)作为一种新工具,该工具通过向语言添加类型来扩展JavaScript。 TS的主要目标是限制类型比较可以使用的值的种类

I think that adding static typing can improve your code quality regardless of whether you like that or not. If you’re not a fan of static typing system, you could try Linter. It’s a great tool which gives you an option to decide which rule will apply.

我认为,无论您是否喜欢,添加静态类型都可以提高代码质量。 如果您不喜欢静态打字系统,可以尝试使用Linter 。 这是一个很棒的工具,可让您选择要应用的规则。

An experienced developer may prefer to use Linter, considering its configurable nature. You can easily catch everything you don’t want to do. You can build a powerful plugin-in which can solve you most of the errors. Have on mind that you cannot prevent logic bugs with this tool.

考虑到Linter的可配置性,有经验的开发人员可能更喜欢使用Linter。 您可以轻松地捕获所有不想做的事情。 您可以构建一个功能强大的插件,该插件可以解决大多数错误。 请记住,您无法使用此工具防止逻辑错误。

Linter is a perfect tool when you’re working on your own. But, what if you’re a part of the team? The team will have to decide democratically on which tool is suitable. There is no tool which can suit for every team. As you can see, there is no space for individual dictators who decide for all!

当您独自工作时,Linter是一个完美的工具。 但是,如果您是团队的一员呢? 该团队将必须民主地决定哪种工具合适。 没有适合每个团队的工具。 正如您所看到的,没有人为所有独裁者做出决定!

In my opinion, static typing tool such as TypeScript and Flow are great tools, and I’m glad they exist. I didn’t use them since I didn’t work in a team. I don’t need them personally because they solve the problems which I don’t have. It’s all about the perspective.

在我看来,诸如TypeScriptFlow之类的静态键入工具是很好的工具,我很高兴它们的存在。 因为没有团队合作,所以我没有使用过它们。 我个人不需要它们,因为它们可以解决我没有的问题。 一切都与视角有关。

I didn’t say I don’t use TS because I don’t have obstacles. However, I solve these problems differently. There can be some intricacies since TS is an abstraction and you won’t learn how JavaScript engine works behind the scenes. In my opinion, I don’t like to use the system that I don’t understand.

我没有说我不使用TS,因为我没有障碍。 但是,我以不同的方式解决这些问题。 由于TS是抽象的,因此可能会有一些复杂性,您将不会了解JavaScript引擎如何在后台工作。 我认为,我不喜欢使用我不了解的系统。

If we want to discuss TypeScript, we’ll first explain the benefits of using static typing system:

如果我们要讨论TypeScript,我们首先将说明使用静态类型系统的好处:

  1. TypeScript is going to catch type related mistakes. It’s necessary to avoid producing type errors. Each developer wants to have a tool which understands his intentions so it can help him to do that correctly.

    TypeScript将捕获与类型相关的错误。 有必要避免产生类型错误。 每个开发人员都希望拥有一个能够理解其意图的工具,从而可以帮助他正确地做到这一点。

  2. TypeScript is super smart when approaches to infer the types. We can say that this helps us to write code which is more flue to other developers. The key intent of this system is a thing we call Type Annotations - a feature built on top of the JavaScript code.

    TypeScript在推断类型时非常聪明。 可以说,这有助于我们编写更适合其他开发人员的代码。 该系统的主要目的是我们称为“类型注释” ( Type Annotations )的一种功能,该功能建立在JavaScript代码之上。

  3. The cool thing is an option to get into the source code. I know this is common for Open Source Software, but full power is displayed if we start using Types Definition files. We can go through those files and understand the flow of the library which we are using - we don’t even need to use docs.

    很酷的事情是进入源代码的一种选择。 我知道这在开源软件中很常见,但是如果我们开始使用类型定义文件,则会显示全部功能。 我们可以浏览这些文件并了解我们正在使用的库的流程-我们甚至不需要使用文档。

  4. Because of those cool Types Definition feature, we can analyse the tooling ecosystem, which can show up live in our IDE. Don’t forget about autocomplete feature and the possibility to adopt the Object-Oriented paradigm.

    由于这些酷的Types Definition功能,我们可以分析工具生态系统,它可以实时显示在我们的IDE中。 不要忘记自动完成功能以及采用面向对象范例的可能性。

  5. TypeScript integration to IDE is impressive, especially in Visual Studio Code. You have to have on mind that both VS Code and TypeScript are Microsoft’s products.

    TypeScript与IDE的集成令人印象深刻,尤其是在Visual Studio Code中 您必须记住,VS Code和TypeScript都是Microsoft的产品。

TypeScript is not the single tool which provides these great features. I mentioned Flow earlier. Flow is a project started back in 2014 by Facebook. It’s not accepted in the community like TypeScript is, but I have no doubt that will be a decent competitor soon.

TypeScript不是提供这些出色功能的唯一工具。 我之前提到过Flow。 Flow是Facebook于2014年启动的一个项目。 它不像TypeScript这样在社区中被接受,但是我毫不怀疑它将很快成为一个不错的竞争对手。

总是有缺点! (There is always a downside!)

Like every system, TypeScript has its caveats too. I said that Type Inference is one of the strongest trump cards which this system has. But, this is an algorithm which depends on some parameters. We cannot rely on him entirely since it’s a sort of a guessing thing, not a guarantee.

像每个系统一样,TypeScript也有其警告。 我说过类型推断是该系统拥有的最强大的王牌之一。 但是,这是一种取决于某些参数的算法。 我们不能完全依靠他,因为这只是一种猜测,而不是保证。

We cannot be sure what will occur at compile time. In this case, your ability to design a system comes to the fore. If you have a poor design, and let the system infer types, you may not get any benefits out of it. If the system isn’t able to guess the variable type, it will set it to the default value - any.

我们不确定在编译时会发生什么。 在这种情况下,您设计系统的能力至关重要。 如果您的设计较差,并且让系统推断类型,则可能无法从中获得任何好处。 如果系统无法猜测变量类型,则会将其设置为默认值-any

If the TS cannot guess the type, that means we have a problem. That can lead to a bad practice since TypeScript recommends to avoid having values which are marked as type any. If you like to operate with type any, a typing system is useless, because the same thing is achieving with pure JavaScript.

如果TS无法猜测类型,则说明我们有问题。 这可能会导致不良做法,因为TypeScript建议避免将值标记为类型任何。 如果您想使用any type操作,则打字系统是没有用的,因为使用纯JavaScript可以实现相同的功能。

Maybe you don’t come from the world of TypeScript, and you cannot understand what I’m talking about, so I’ll give a simple example:

也许您不是来自TypeScript的世界,并且您无法理解我在说什么,所以我举一个简单的例子

Type Inference in TypeScript
TypeScript’s Type Inference in action
TypeScript的类型推断在起作用

As we can see, Type Inference guesses the type based on the array’s initialization. From now on, this array can only contain numbers:

如我们所见,类型推断根据数组的初始化来猜测类型。 从现在开始,此数组只能包含数字:

Example of the wrong usage of Type Inference in TypeScript
Type Inference has its power
类型推断有其力量

See, we cannot add a string, or any other type to the array, only numbers. Things come disordered if we do the next thing:

请参阅,我们不能将字符串或任何其他类型添加到数组中,只能添加数字。 如果我们做下一件事情,事情就会变得混乱:

Example of the Type Inference’s default type ‘any’
Type Inference’s default type is ‘any’
类型推断的默认类型为“任意”

The system cannot guess what we’re trying to do because we initialize an empty array. In those case, TypeScript will set a default value, which is any. To solve this problem, we have to annotate type:

系统无法猜测我们要做什么,因为我们初始化了一个空数组。 在这种情况下,TypeScript将设置一个默认值any 。 为了解决这个问题,我们必须注释类型:

Type Annotation in TypeScript
TypeScript’s Type Annotation in action
TypeScript的类型注释在起作用

Conclusion: Each type without annotation no matter is it our code or library we use, could potently cause a problem, and lead to a false sense of security. We have to be very careful with Type Inference.

结论:每种没有注释的类型,无论是我们使用的代码还是库,都可能会引起问题,并导致错误的安全感。 我们必须非常小心类型推断。

翻译自: https://medium.com/javascript-in-plain-english/how-to-adopt-a-static-typing-c59de0973cca

静态类型 强类型

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值