简体   繁体   English

为什么 tslint 中不允许按位运算符?

[英]Why are bitwise operators not allowed in tslint?

我们不能在模板中使用按位运算符,但为什么 TypeScript 代码中的 tslint 不允许使用位运算符?

"no-bitwise": true,

Bitwise operators are often typos - for example bool1 & bool2 instead of bool1 && bool2.按位运算符通常是错别字 - 例如 bool1 & bool2 而不是 bool1 && bool2。 They also can be an indicator of overly clever code which decreases maintainability.它们也可能表明代码过于聪明,从而降低了可维护性。

https://palantir.github.io/tslint/rules/no-bitwise/ https://palantir.github.io/tslint/rules/no-bitwise/

Linters exist for multiple reasons: to help maintain consistent, clean and readable code, catch developer mistakes (eg unreachable code or unused variables) and to warn you about potentially bad practices even though they may technically be allowed. Linter 的存在有多种原因:帮助维护一致、干净和可读的代码,捕捉开发人员的错误(例如无法访问的代码或未使用的变量)并警告您潜在的不良做法,即使它们在技术上是允许的。

As mentioned in the TSLint documentationTSLint 文档中所述

Bitwise operators are often typos - for example bool1 & bool2 instead of bool1 && bool2 .按位运算符通常是错别字 - 例如bool1 & bool2而不是bool1 && bool2 They also can be an indicator of overly clever code which decreases maintainability.它们也可能表明代码过于聪明,从而降低了可维护性。

Since these types of typos are so much more common than actual valid uses of bitwise operators, TSLint forbids them by default .由于这些类型的拼写错误比按位运算符的实际有效使用更常见,TSLint默认禁止它们。

Unless you're working on an application whose sole purpose is to do bitwise operations, it's best to keep the rule enabled (because just like anyone else you are prone to making this kind of typo).除非您正在处理的应用程序的唯一目的是进行按位运算,否则最好保持启用规则(因为就像其他人一样,很容易犯这种类型的错字)。 If however you do have a valid case to use bitwise, then disable the rule temporarily just for that line or block of code, like this:但是,如果您确实有一个有效的情况可以按位使用,则暂时针对该行或代码块禁用该规则,如下所示:

/* tslint:disable:no-bitwise */
const redColor = (decimalColor & 0xff0000) >> 16;
const greenColor = (decimalColor & 0x00ff00) >> 8;
const blueColor = decimalColor & 0x0000ff;
/* tslint:enable:no-bitwise */

don't forget to re-enable the rule!不要忘记重新启用规则!

or for a single line:或单行:

// tslint:disable-next-line:no-bitwise
const redColor = (decimalColor & 0xff0000) >> 16;

If using ESLint, see documentation here如果使用 ESLint,请参阅此处的文档

If you look at the Docs如果您查看Docs

"Bitwise operators are very rare in JavaScript programs" “按位运算符在 JavaScript 程序中非常少见”

anyhow you can disable the bitwise option to stop the warnings.无论如何,您可以禁用按位选项来停止警告。

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

相关问题 Tslint Angular:不允许使用窗口 - Tslint Angular: Usage of window is not allowed 为什么 tslint 会删除对象键? - Why does tslint remove object-keys? tslint.json问题“不允许输入”。 如何确保配置符合您在tslint.json上设置的设置 - “type is not allowed” tslint.json issue. How to Ensure the configuration meets with what you set up on tslint.json Tslint - 类型简单推断 - 为什么在这里包含类型是不好的做法? - Tslint - type trivially inferred - Why is it bad practice to include the type here? 为什么 PhpStorm 不显示所有 TSLint 规则? - Why doesn't PhpStorm show all TSLint rules? 对TSLint的怀疑 - Doubts with TSLint 为什么作业左侧不允许使用“ this” - Why 'this' is not allowed on the left side of assignment 为什么在启用 no-unsafe-any 时 tslint 将强类型参数的属性标记为 any - Why is tslint flagging properties of strongly typed parameters as any when no-unsafe-any is enabled 为什么 webstorm (TSLINT) 中的 angular 在 model 中的“_id”被定义时向我发送警报? - why angular in webstorm (TSLINT) send me alert when “_id” in model is definied? 为什么“ npm install ”命令在安装 Angular 项目时会警告“npm WARN deprecated tslint@6.1.3:”? - Why “ npm install ” command warns “npm WARN deprecated tslint@6.1.3:” while installing Angular project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM