typescript / beginner
Snippet
Readonly Properties
The readonly modifier makes a property immutable after its initial assignment, preventing accidental changes to critical data.
snippet.ts
1
2
3
4
interface User {readonly id: number;name: string;}
Breakdown
1
readonly id: number
The 'id' property can be read but not reassigned after the object is created.
2
name: string
A standard property that can be updated normally.