TypeScript代码:
private goY : string = 'www.baidu.com';
会导致tslint报错:
Type string trivially inferred from a string literal, remove type annotation (no-inferrable-types)
tslint觉得自己根据右边的"www.baidu.com"判断出requestUrl的类型是string,所以,认为再写string是多此一举。
解决方法:tslint.json添加"ignore-properties"。不推断类的属性(字段)。
"no-inferrable-types": [
true,
"ignore-params",
"ignore-properties"
],
-------------------------------------------------
TypeScript无类型推断配置。
译文:
规则:无推断类型
不允许对初始化为数字,字符串或布尔值的变量或参数进行显式类型声明。
合理
编译器容易推断的显式类型使得代码更加冗长。
标记:
TS独有
已经固定
配置
可以提供两个参数:
ignore-params
允许为函数参数指定不可推出的类型注释。当与typedef
规则组合时,这可以很有用。ignore-properties
允许为类属性指定不可推出的类型注释。
例子
"no-inferrable-types": true
"no-inferrable-types": [true, "ignore-params"]
"no-inferrable-types": [true, "ignore-params", "ignore-properties"]
样板
{ "type": "array", "items": { "type": "string", "enum": [ "ignore-params", "ignore-properties" ] }, "minLength": 0, "maxLength": 2 }