

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Object Literal Types
incomplete
2: Extra Properties
incomplete
3: Optional Object Properties
incomplete
4: Empty Object Type
incomplete
5: Discriminated Unions
incomplete
6: Sets
incomplete
7: Maps
incomplete
8: Dynamic Keys
incomplete
9: Dynamic Default Properties
incomplete
10: PropertyKey
incomplete
11: Readonly Modifier
incomplete
12: 'As Const' and Object.freeze
incomplete
13: Satisfies
incomplete
14: Function Overloads
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
The readonly modifier is very similar to the const keyword in JavaScript. It's an added feature of TypeScript that lets us mark object properties as read-only, meaning they can't be changed after initialization.
Normal object properties are fully mutable, but if we use readonly, we can make a property immutable:
type Point = {
readonly x: number;
y: number;
};
Now we can create a new point like this:
const point: Point = {
x: 10,
y: 20,
};
And we can update the y property just fine:
point.y = 30; // OK
But if we try to update the x property, TypeScript will throw an error:
// Error: Cannot assign to 'x' because it is a read-only property
point.x = 15;
readonly is pretty awesome. Just keep in mind that you probably want readonly properties less often than you want const variables, because re-creating entire objects and copying all the fields can become painful and verbose if you do it too often.
Support.ai interns introduced a bug in our preferences logic. Fix it!