Basics

Challenges

Classes

Cookies

Debugging

Events

External Files

Flow Control

Forms

Functions

Html Elements

Installation

Interfaces

Keywords

Modules

Namespaces

Operators

Reference Files

String

Types

TypeScript - readonly

Some properties should only be modifiable when an object is first created. You can specify this by putting readonly before the name of the property.

readonly is a keyword that can only be used in a class. A const can be used anywhere.

The easiest way to remember whether to use readonly or const is to ask whether you’re using it on a variable or a field/property.

Variables use const whereas fields/properties use readonly.

Properties declared as readonly can only be set in a constructor or when they are declared and initialized.

 

 

You can also assign properties of a class to be readonly but still have the constructor set them via arguments. Once a user instantiates the object and gives values for these properties, they cannot be changed. readonly is used in Classes only.