繁体   English   中英

TSLint ESLint-当函数作为属性访问时发出警告?

[英]TSLint ESLint - warn when function accessed as property?

我已经犯了几次这个错误-想知道是否有ESLint或TSLint规则可以发现它

if (this.isBrowser && this.imageURL) {.....}

private isBrowser(): boolean{
    return isPlatformBrowser(this.platformId);
}

使用this.isBrowser将始终返回true,因为它是一个函数,这是事实。 我必须使用get isBrowser() {}this.isBrowser()

ESLint或TSLint可以检查并警告将对函数的调用写为属性访问器吗?

短绒可以处理属性获取器的唯一情况是当它们可能是无操作时,存在TSLint / ESLint no-unused-expression规则

this.isBrowser; // causes linter error

柴的断言就是这种情况。 在任何其他情况下, this.isBrowser都不是禁止操作。

if (this.isBrowser)是检查isBrowser成员是否真实的有效代码。 if (this.isBrowser) this.isBrowser()if (this.isBrowser) this.isBrowser()该方法可能对方法有效。

使用TypeScript解决此问题的一种方法是不要懒于条件

if (this.isBrowser === true && this.imageURL) {.....}

如果isBrowser是一个函数,这将导致类型错误。

这是不确定的代码样式可能导致的问题。 如果一个检查是否为浏览器的方法称为isBrowser ,那么如何调用布尔属性? 方法和属性可以混淆并且不能共存这一事实表明,一个返回布尔值的方法可能具有不同的名称,例如getIsBrowser ,而isBrowser保留为布尔值。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM