我想在我的程序中使用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?,但是不知道为什么 TypeScript 使用 const
关键字时会出现这种错误。