Why a class member cannot have the 'const' keyword in TypeScript?
I cannot find any usefull information about it at TypeScript documentation website.
With TypeScript being an extension of JavaScript, the language naturally supports let and const .
const scope is defined as 'block scoped' (the scope of which, is restricted to the block in which it is declared). MDN documentation: Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through re-assignment, and it can't be redeclared.
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
const is a new keyword which declares a variable as constant over time.
Why a class member cannot have the 'const' keyword in TypeScript?
const
does not imply deep immutability so the following is valid:
const foo:any = {};
foo.bar = 123; // Okay
In that sense readonly makes better sense for class members and that is supported : https://basarat.gitbooks.io/typescript/content/docs/types/readonly.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With