-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.ReadOnlyModifier.ts
36 lines (27 loc) · 1.22 KB
/
24.ReadOnlyModifier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// The `readonly` modifier ensures that once a property is set, it cannot be changed.
// This is useful for creating immutable objects or properties.
// Using the `readonly` modifier with object types
type Coordinates = {
readonly latitude: number;
readonly longitude: number;
};
// Creating an object of type `Coordinates`
const location: Coordinates = { latitude: 10, longitude: 20 };
// Trying to reassign the entire object will result in an error
// location = { latitude: 30, longitude: 40 }; // This will throw an error!
// Trying to modify individual properties will also result in an error
// location.latitude = 30; // This will throw an error!
// location.longitude = 40; // This will throw an error!
console.log(`Latitude: ${location.latitude}, Longitude: ${location.longitude}`); // 10, 20
// Using the `readonly` modifier in classes
class Creature {
readonly species: string;
constructor(species: string) {
this.species = species;
}
}
// Creating an instance of the `Creature` class
const lion = new Creature('Lion');
// Trying to modify the `species` property will result in an error
// lion.species = 'Tiger'; // This will throw an error!
console.log(`The creature is a: ${lion.species}`); // 'Lion'