capypad
0 day streak
typescript / beginner
Snippet

Readonly Modifier

The 'readonly' keyword makes a property immutable. It can only be assigned a value at declaration or within the constructor of the class.

snippet.ts
typescript
1
2
3
4
class Car {
readonly brand: string;
constructor(b: string) { this.brand = b; }
}
Breakdown
1
readonly brand: string
Declares a property that cannot be changed after the object is created.
2
this.brand = b
Setting the value in the constructor is allowed for readonly properties.