typescript - 需要只读修饰符
问题描述
在接下来的课程中,即使我在课程中重新分配值,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
变量?
解决方案
这里好像没有什么问题...
private readonly timestamp: boolean = false;
constructor() {
this.timestamp = true;
}
双重初始化可能不应该传递一些 lint 标志,但似乎不是你的问题。
你可以尝试一些愚蠢的事情:
将“时间戳”重命名为其他名称
npm 我
重启IDE
推荐阅读
- c# - Nmodbus4 检查连接
- sympy - sympy:根据变量重新排列方程
- macos - ZWJ(零宽度连接器)序列表情符号显示为 2 个表情符号
- r - dplyr::select 基于存储在 attributes() 中的信息
- c# - 为什么我的 Console.WriteLine(", '{0}'", String); 总是收到 System.IndexOutOfRangeException 陈述?
- c# - 如何将通知从 sql 数据库推送到 Flutter App
- angular - Ngxs @Select 装饰器可以返回 BehaviorSubject 而不是 Observable 吗?
- reactjs - 如何在嵌套函数中设置无状态函数中的状态?
- html - 我无法重现模型渐变
- ios - 如何访问我的应用程序的派生数据文件夹本身?