Type string trivially inferred from a string literal, remove type annotation (no-inferrable-types)

TypeScript代碼:json

private goY : string = 'www.baidu.com';

會致使tslint報錯:swift

Type string trivially inferred from a string literal, remove type annotation (no-inferrable-types)。函數

tslint以爲本身根據右邊的"www.baidu.com"判斷出requestUrl的類型是string,因此,認爲再寫string是畫蛇添足。spa

解決方法:tslint.json添加"ignore-properties"。不推斷類的屬性(字段)。code

"no-inferrable-types": [
      true,
      "ignore-params",
      "ignore-properties"
    ],

-------------------------------------------------ip

TypeScript無類型推斷配置。rem

譯文:字符串

規則:無推斷類型

不容許對初始化爲數字,字符串或布爾值的變量或參數進行顯式類型聲明。編譯器

合理string

編譯器容易推斷的顯式類型使得代碼更加冗長。

標記:

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
}
相關文章
相關標籤/搜索