I want to use const
keyword in my program.
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
}
}
}
Question1: why the class member does not have the const keyword in typescript?
Question2: When I use
static readonly CONSTANT = 'constant';
and assign it in
this.result = this.CONSTANT;
it displays error. why so?
I have followed this post How to implement class constants in typescript? but don't get the answer why typescript is displaying this kind of error with const
keyword.