【发布时间】:2018-10-30 05:23:42
【问题描述】:
我犯过几次这个错误 - 想知道是否有 ESLint 或 TSLint 规则可以发现它
if (this.isBrowser && this.imageURL) {.....}
private isBrowser(): boolean{
return isPlatformBrowser(this.platformId);
}
使用this.isBrowser 将始终返回true,因为它是一个函数的事实是真实的。我要么必须使用get isBrowser() {} 或this.isBrowser()
ESLint 或 TSLint 是否可以检查并警告对函数的调用正在编写为属性访问器?
【问题讨论】:
-
它不是“被称为 getter”,你只是在访问一个碰巧可以调用的属性。这不一定是 JS 或 TS 中的错误,因此 linter(或编译器)不会发现它;您要么必须显式分配
const isBrowser: boolean = this.isBrowser;以获得编译器警告,要么可能更好的是通过测试而不是 linting 来验证这一点。 -
@jonrsharpe 已编辑,您是对的,它不是吸气剂。 linters 发现了很多不是错误的东西(无尾随空格、无变量关键字、首选常量),所以我想这可能是其中之一
标签: javascript typescript eslint tslint