【问题标题】:tslint prefer-const warning when using dot notation使用点表示法时的 tslint prefer-const 警告
【发布时间】:2018-09-17 00:23:17
【问题描述】:
interface obj {
  bar: string
}

function randomFunction() {
  let foo: obj = { bar: "" }
  foo.bar = "hip"
}

let snack: obj = { bar: "" }
snack.bar = "hop"

我从 tslint 收到此警告:

标识符“foo”永远不会被重新分配;使用“常量”而不是“让”。 (首选常量)

有趣的是,在变量snack 的第二种情况下我没有收到此警告。

我可以用 /* tslint:disable: prefer-const */

我没有在tslint project 上找到任何错误报告。 由于我是打字稿的新手,我想知道:我这里有什么问题吗?

【问题讨论】:

标签: typescript tslint


【解决方案1】:

tslint 要求您将 let 更改为 const,因为标识符 foo 未被重新分配。

可以改写const来消除错误:

const foo: obj = { bar: "" };
foo.bar = "hip";

请注意,const 修饰符只是意味着您不能重新分配标识符:

 const foo = { bar: "" };
 foo = { bar: "" }; // error

它不会使对象本身成为只读的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 2021-05-20
    • 1970-01-01
    • 2017-12-20
    • 2019-04-17
    • 2017-12-04
    • 2017-10-24
    • 2020-08-06
    相关资源
    最近更新 更多