首页 > 解决方案 > 需要只读修饰符

问题描述

在接下来的课程中,即使我在课程中重新分配值,tslint readonly-keyword也不允许我拥有常规变量。timestamp我需要将变量timestamp默认为false并在需要时重新分配。

export default class A {
    private timestamp: boolean = false;

    withTimestamp() {
        this.timestamp = true;
    }
}

如果我执行以下操作,

export default class A {
    private readonly timestamp: boolean = false;

    constructor() {
        this.timestamp = true;
    }
}

tsserver警告我

timestamp已声明但它的值永远不会被读取。

如何this.timestamp在构造函数中不访问类的相同timestamp变量?

标签: typescripttslint

解决方案


这里好像没有什么问题...

private readonly timestamp: boolean = false;

constructor() {
    this.timestamp = true;
}

双重初始化可能不应该传递一些 lint 标志,但似乎不是你的问题。

你可以尝试一些愚蠢的事情:

  • 将“时间戳”重命名为其他名称

  • npm 我

  • 重启IDE


推荐阅读