简体   繁体   English

Typescript 中的类型推断与显式类型声明

[英]Type inference vs. explicit type declaration in Typescript

I've come across several different instances of code where variables are declared with an explicit type even though the inferred type is obvious:我遇到过几个不同的代码实例,其中使用显式类型声明变量,即使推断类型很明显:

Example: loading: boolean = false or name: string = "John" or count: number = 0 etc.示例: loading: boolean = falsename: string = "John"count: number = 0等。

TSLint favors the inferred type over the explicit type, so I'm wondering is this just a stylistic issue? TSLint 更喜欢推断类型而不是显式类型,所以我想知道这只是一个文体问题吗? Do these explicit types even matter during runtime?这些显式类型在运行时是否重要?

Declared types do not matter at all at runtime. 声明的类型在运行时根本不重要。 When the Javascript is generated all types will be removed since Javascript does not support specifying variable types. 生成Javascript时,所有类型都会被删除,因为Javascript不支持指定变量类型。

As to why TSLint prefers type inference ove explicit typing I would say it is probably to keep things DRY (don't repeat yourself). 关于TSLint为什么更喜欢类型推断而不是显式类型的原因,我想说这可能是为了使事情保持干燥(不要重复自己)。 Since the compiler can do the typing for you there is no reason to add more noise to the code. 由于编译器可以为您执行键入操作,因此没有理由为代码增加更多干扰。

In your example it is just about style, hence, it has not impact to your code from compilation perspective. 在您的示例中,它只是关于样式,因此,从编译角度看,它对代码没有影响。 Be aware this is for the cases where the variable value explicitly defines its type, which might make your code complicated to read in cases of resigning values from other variables. 请注意,这是在变量值显式定义其类型的情况下使用的,这可能会使您的代码在从其他变量重设值的情况下读取起来很复杂。

In other words it might be better you do: 换句话说,您最好这样做:

name: string = "John"
bday: Date = "1980/01/10" //the compiler says there is an error

And avoid: 并避免:

name = "John"
bday = "1980/01/10" //no compiling error, but it should be new Date("1980/01/10")

Note: Undefined types will always be considered as any. 注意:未定义的类型将始终被视为任何类型。

Most of today's javascript community people would support inference motivating their choice that type inference is somehow easier to read, but ease of reading is not something measurable, it's just a matter of personal taste. 当今大多数javascript社区人士都会支持推理,以促使他们选择以某种方式更易于阅读类型推理,但是阅读的便捷性却无法衡量,这只是个人喜好问题。 And think twice before working with people who would motivate their position based solely on their taste. 在与仅根据自己的品味来激发自己的位置的人们合作之前,请三思而后行。

Be open try both approaches pick one that suits you best or go with team's decision. 保持开放,尝试两种方法都选择最适合您的方法,或者根据团队的决定进行。

I personally prefer explicit types, my measurable observations tell me that explicit types improve code metric related to code simplicity or verbosity, you don't have to put effort into figuring out what type are you working with, plus you are less probable to make a mistake. 我个人更喜欢显式类型,我的可测观察结果告诉我,显式类型可以改善与代码简单性或冗长程度相关的代码指标,您不必费力确定要使用哪种类型,而且您不太可能做出错误。

From Should you annotate or let TypeScript infer the types? From你应该注释还是让 TypeScript 推断类型?

The best general guideline for whether to annotate code or not is whether the expression/variable you're annotating is a "boundary" or not.是否注释代码的最佳一般准则是您注释的表达式/变量是否是“边界”。 Boundaries usually refer to the exported functions/classes/etc.边界通常是指导出的函数/类/等。 of your various modules.您的各种模块。 For these, you likely want to be explicit as to what you're expecting to make your APIs more clear.对于这些,您可能希望明确说明您期望什么使您的 API 更加清晰。

However, functions and variables internal to a specific module likely don't need to be annotated但是,特定模块内部的函数和变量可能不需要注释

From When to add types and when to infer in TypeScript何时添加类型以及何时在 TypeScript 中推断

Add types to all function declarations.将类型添加到所有函数声明中。

not add explicit types to variables不向变量添加显式类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 2.0.x与2.1.x,类型定义由switch / case语句过滤 - typescript 2.0.x vs. 2.1.x, type definition filtered by switch/case statement 打字稿中声明部分的类型错误(Angular/React) - Type error in declaration part in typescript (Angular/React) 有没有办法将类型声明中特定键的值用作同一声明中另一个键的类型(在 Typescript 中)? - Is there a way to use the value from a specific key in a type declaration as the type of another key in that same declaration (in Typescript)? 角度打字稿中非模块的类型声明文件 - type-declaration file for non-module in angular typescript 在打字稿中的类型声明期间在类内调用类 - Calling a class inside a class during type declaration in typescript TypeScript 无法编译,因为“any”的类型声明失去了类型安全性 - TypeScript Failed to compile because Type declaration of 'any' loses type-safety 打字稿:类型不可分配给类型 - Typescript : Type is not assignable to type 无法在 eslint 中为 typescript linting 关闭规则显式函数返回类型 - Can not get rule explicit-function-return-type to turn off in eslint for typescript linting 泛型 Type(T) 与 typescript 中的任何类型有什么区别 - What are the difference between generic Type(T) vs any in typescript Angular中使用Typescript的常量变量与枚举类型 - constant variables vs enum type in Angular using Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM