【发布时间】:2018-03-28 19:15:06
【问题描述】:
我想在我的程序中使用 const 关键字。
export class Constant {
let result : string;
private const CONSTANT = 'constant'; //Error: A class member cannot have the const keyword.
constructor () {}
public doSomething () {
if (condition is true) {
//do the needful
}
else
{
this.result = this.CONSTANT; // NO ERROR
}
}
}
问题1:为什么类成员在 typescript 中没有 const 关键字?
问题2:当我使用
static readonly CONSTANT = 'constant';
并赋值给
this.result = this.CONSTANT;
它显示错误。为什么会这样?
我关注了这篇帖子How to implement class constants in typescript?,但不明白为什么打字稿会用const关键字显示这种错误。
【问题讨论】:
标签: typescript constants