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

原代码:

public isCollapse: boolean = true

以上代码会报错:Type string trivially inferred from a string literal, remove type annotation (no-inferrable-types)

tslint觉得自己根据右边的true判断出isCollapse的类型是boolean,所以,认为再写boolean是多此一举。

解决方法:tslint.json添加”ignore-properties”。不推断类的属性(字段)。

  • ignore-params允许为函数参数指定不可推出的类型注释。当与typedef规则组合时,这可以很有用。
  • ignore-properties 允许为类属性指定不可推出的类型注释。
"parserOptions": {
  "ecmaVersion": 2020,
  // 添加在此
  "no-inferrable-types": [
    true,
    "ignore-params",
    "ignore-properties"
  ]
},
"rules": {
  "semi": [
    2,
    "never"
  ],
  "lines-between-class-members": [
    2,
    "always",
    {
      "exceptAfterSingleLine": true
    }
  ],
  "global-require": 0
},

给TA打赏
共{{data.count}}人
人已打赏
前端问答

关闭Expected 'this' to be used by class method这个规则

2020-12-15 15:34:42

前端库/框架

Yarn入门教程 - 如何安装?

2020-11-12 9:54:47

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索