eslint成员排序陷阱-先出现的声明会导致错误
我有这个代码:
export class RouteParamsService {
private routeParamsChangeSource = new ReplaySubject<Params>() // lets call this line 1
routeParamsChange$ = this.routeParamsChangeSource.asObservable() // lets call this line 2
... etc
}
如果我将第 1 行放在第 2 行之前,则会出现错误:
@typescript-eslint/member-ordering 成员 routeParamsChange$ 应该在所有私有实例字段定义之前声明
如果我将第 2 行放在第 1 行之前,则会出现错误:
属性 routeParamsChangeSource 在初始化之前使用
我理解这两个错误以及为什么我会得到它们。但是,是否有一条规则可以放宽规则,但仅当您最终陷入这样的陷阱时?我知道我可以做到,eslint-disable-line @typescript-eslint/member-ordering但我不想每次遇到这个问题时都必须这样做(我经常遇到这个问题)。
我也不想公开 routeParamsChangeSource。
有任何想法吗?谢谢
回答
该@typescript-eslint/member-ordering皮棉规则不理解当前字段之间的依赖关系。
正如你所理解的 - 这种依赖会产生一个复杂的排序问题,社区中没有人有足够的动力去解决这个问题。
您可以在此处查看跟踪它的问题:https : //github.com/typescript-eslint/typescript-eslint/issues/2882
该项目欢迎贡献——如果这是一个对你很重要的问题。
至于实际的解决方法或修复。
禁用评论是一个很好的临时措施。
另一种选择是将依赖项移动到构造函数中:
export class RouteParamsService {
private routeParamsChangeSource = new ReplaySubject<Params>();
routeParamsChange$;
constructor() {
this.routeParamsChange$ = this.routeParamsChangeSource.asObservable();
}
}