gpt4 book ai didi

angular - 为什么 tslint 中不允许使用按位运算符?

转载 作者:太空狗 更新时间:2023-10-29 16:58:47 24 4
gpt4 key购买 nike

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

"no-bitwise": true,

最佳答案

Linters 的存在有多种原因:帮助保持一致、干净和可读的代码,发现开发人员的错误(例如无法访问的代码或未使用的变量)并警告您潜在的不良做法,即使它们在技术上可能是允许的。

TSLint documentation 中所述

Bitwise operators are often typos - for example bool1 & bool2 instead of bool1 && bool2. They also can be an indicator of overly clever code which decreases maintainability.

由于这些类型的拼写错误比按位运算符的实际有效使用更为常见,TSLint 默认禁止它们。

除非您开发的应用程序的唯一目的是进行按位运算,否则最好启用该规则(因为就像其他任何人一样也容易犯这种错字) .但是,如果您确实有一个有效的大小写可以按位使用,那么disable the rule temporarily 只是那行或代码块,像这样:

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

不要忘记重新启用规则!

或单行:

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

如果使用 ESLint,请参阅 documentation here

关于angular - 为什么 tslint 中不允许使用按位运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47622824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com