typescript / beginner
Snippet
Readonly Properties
The 'readonly' modifier makes a property immutable after the object is first initialized, preventing accidental changes to sensitive data.
snippet.ts
1
2
3
4
5
6
type Config = {readonly apiKey: string;version: number;};const myConfig: Config = { apiKey: "XYZ", version: 1 };
Breakdown
1
readonly apiKey: string;
Marks the property as read-only; it cannot be reassigned after object creation.
2
version: number;
A standard property that remains mutable.