Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'const' keyword in TypeScript

Tags:

typescript

Why a class member cannot have the 'const' keyword in TypeScript?

I cannot find any usefull information about it at TypeScript documentation website.

like image 277
tautvisv Avatar asked Mar 21 '16 22:03

tautvisv


People also ask

Can I use const in TypeScript?

With TypeScript being an extension of JavaScript, the language naturally supports let and const .

What is the scope of const in TypeScript?

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.

What does keyword const mean?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

What is const in angular?

const is a new keyword which declares a variable as constant over time.


1 Answers

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

like image 58
basarat Avatar answered Oct 17 '22 07:10

basarat