TSLint:Backbone get() 在拥有模型含义之外调用
TSLint: Backbone get() called outside of owning model meaning
我正在使用 Microsoft 的 tslint-microsoft-contrib
tslint 配置,我对它非常满意。但是,有一条规则会警告我有关我的代码的信息。我不明白规则描述文本或如何更优雅地解决这个问题。
[tslint] Backbone get() called outside of owning model:
this.client.get('locations') (no-backbone-get-set-outside-model)
代码:
import * as Redis from 'ioredis';
import config from './config';
export class RedisWrapper {
private client: Redis.Redis
constructor(redisUrl: string) {
this.client = new Redis(redisUrl)
}
public async getLocations(): ILocation[] {
const locationsResponse: string = await this.client.get('locations')
}
}
在这一行中弹出 tslint 警告:const locationsResponse: string = await this.client.get('locations')
问题:
最初我在项目的另一个地方遇到过这个问题,我认为我应该用 typedef 编写包装器方法,但我也无法让 tslint 对此感到满意。谁能告诉我这条规则是什么意思,我该如何解决?
我将引用 HamletDRC(来自 Microsoft 团队),他很好地解释了规则本身:
The point of the no-backbone-get-set-outside-model rule is to make
sure that you don't invoke dynamically dispatched methods that the
compiler cannot enforce correctness on. For example, the compiler will
not complain if you type route.params.get('id'),
route.params.get('ID'), route.params.get('Id') but only one of those
invocations will actually work at runtime. The design advice is to
define a statically typed "getId(): number" method on the RouteParams
object so the compiler can enforce these calls. So, in my opinion the
rule actually has found an issue in your code that you should fix (but
see my second point :) )
来源:https://github.com/Microsoft/tslint-microsoft-contrib/issues/123
在这种特定情况下,可以像这样扩展 Redis class:
export class RedisWrapper extends Redis {
public async getLocations(): Promise<ILocation[]> {
const response: string = await this.get('locations');
if (response == null || response.length === 0) { return []; }
return <ILocation[]>JSON.parse(response);
}
}
我正在使用 Microsoft 的 tslint-microsoft-contrib
tslint 配置,我对它非常满意。但是,有一条规则会警告我有关我的代码的信息。我不明白规则描述文本或如何更优雅地解决这个问题。
[tslint] Backbone get() called outside of owning model: this.client.get('locations') (no-backbone-get-set-outside-model)
代码:
import * as Redis from 'ioredis';
import config from './config';
export class RedisWrapper {
private client: Redis.Redis
constructor(redisUrl: string) {
this.client = new Redis(redisUrl)
}
public async getLocations(): ILocation[] {
const locationsResponse: string = await this.client.get('locations')
}
}
在这一行中弹出 tslint 警告:const locationsResponse: string = await this.client.get('locations')
问题:
最初我在项目的另一个地方遇到过这个问题,我认为我应该用 typedef 编写包装器方法,但我也无法让 tslint 对此感到满意。谁能告诉我这条规则是什么意思,我该如何解决?
我将引用 HamletDRC(来自 Microsoft 团队),他很好地解释了规则本身:
The point of the no-backbone-get-set-outside-model rule is to make sure that you don't invoke dynamically dispatched methods that the compiler cannot enforce correctness on. For example, the compiler will not complain if you type route.params.get('id'), route.params.get('ID'), route.params.get('Id') but only one of those invocations will actually work at runtime. The design advice is to define a statically typed "getId(): number" method on the RouteParams object so the compiler can enforce these calls. So, in my opinion the rule actually has found an issue in your code that you should fix (but see my second point :) )
来源:https://github.com/Microsoft/tslint-microsoft-contrib/issues/123
在这种特定情况下,可以像这样扩展 Redis class:
export class RedisWrapper extends Redis {
public async getLocations(): Promise<ILocation[]> {
const response: string = await this.get('locations');
if (response == null || response.length === 0) { return []; }
return <ILocation[]>JSON.parse(response);
}
}