无比打字与拼英打字_打字稿类型和注释

无比打字与拼英打字

The Type System is the brains behind TypeScript. It helps catch errors during development and analyzes our code through type annotation and type inference. To be able to use TypeScript effectively, you must first be aware of what a Type is and what types are available.

类型系统是TypeScript背后的大脑。 它有助于在开发过程中捕获错误,并通过类型注释和类型推断来分析我们的代码。 为了能够有效地使用TypeScript,您必须首先知道什么是类型以及哪些类型可用。

什么是类型? (What are Types?)

A Type is a description of a piece of data. TypeScript supports all of the Types in JavaScript with the addition of enumeration. Some examples of Types include:

类型是对数据的描述。 TypeScript除了枚举外,还支持JavaScript中的所有Types。 类型的一些示例包括:

  • Boolean

    布尔型
  • Number

  • String

  • Array

    数组
  • Object

    目的
  • Void

    空洞
  • Any

    任何

Refer to the documentation for a complete list.

请参阅文档以获取完整列表。

Types add necessary context, which allows the type system to properly analyze your code and display appropriate errors before it compiles.

类型添加了必要的上下文,这使类型系统可以在编译之前正确分析代码并显示适当的错误。

如何使用类型? (How to use Types?)

Now that we know what Types are and why they are essential let’s discuss how to use them. TypeScript is very smart. As you write code, the type system is attempting to guess which data types you use if you don’t explicitly annotate them. This is called Type Inference.

现在我们知道类型是什么,为什么它们是必不可少的,让我们讨论如何使用它们。 TypeScript非常聪明。 在编写代码时,类型系统会尝试猜测未使用显式注释的数据类型。 这称为类型推断

Image for post
Type Inference in action
进行类型推断

Type inference works when your variable declaration and variable initialization happens on the same line, as seen in the example above. If you were to declare your variable on one line and assign it a value on another, TypeScript would no longer be able to guess the type you intended to use. The variable would instead default to the :any Type, which, if used too often, can defeat the purpose of the type system.

如上例所示,当变量声明和变量初始化发生在同一行时,类型推断起作用。 如果要在一行上声明变量,然后在另一行上为其赋值,则TypeScript将不再能够猜测要使用的类型。 相反,该变量将默认为:any Type,如果使用过多,可能会破坏类型系统的作用。

If we wanted to declare a variable without knowing it’s value right away, we would need to use Type Annotation. Type annotation ensures we are working with the type system to deliver accurate error messages. If you are working on an extensive application and you see a variable you want to reassign, so long as you are using the keywords let or var you are allowed to do so in JavaScript. In TypeScript, however, because that variable was annotated, you would get an error ensuring that the value remains a consistent type.

如果我们想在不立即知道变量值的情况下声明变量,则需要使用Type Annotation 。 类型注释可确保我们与类型系统一起提供准确的错误消息。 如果您正在开发广泛的应用程序,并且看到要重新分配的变量,则只要使用关键字letvar ,就可以在JavaScript中使用它。 但是,在TypeScript中,由于对该变量进行了注释,因此会出错,以确保该值保持一致的类型。

Image for post
Type Annotation in action
实际使用类型注释

When writing a function, it is required to annotate the parameters as well as the expected output. For example, a function whose purpose is to find the sum of two numbers would look like the example below.

编写函数时,需要注释参数以及预期的输出。 例如,一个旨在查找两个数字之和的函数看起来像下面的示例。

findSum= (value1: number, value2: number): number =>{return value1 + value2}

结论 (Conclusion)

Type Annotations and Type inferences are vital features of TypeScript that help us write clean, consistent code for ourselves and other developers. TypeScript has more features that allow us to create Types and Type Interfaces, but those are more complex and deserve a dedicated blog post. As always, the official documentation, linked at the top, contains all of the information above and more. Thank you for reading.

类型注释和类型推断是TypeScript的重要功能,可帮助我们为自己和其他开发人员编写简洁,一致的代码。 TypeScript具有更多的功能,可让我们创建“类型”和“类型接口”,但这些功能更为复杂,因此值得撰写专门的博客文章。 与往常一样,链接在顶部的官方文档包含上面所有的信息以及更多信息。 感谢您的阅读。

Image for post

翻译自: https://medium.com/swlh/typescript-types-and-annotations-4ef9d912c92e

无比打字与拼英打字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值