Readonly and Partial
Readonly and Partial are two helper types you will use a lot.
Readonly freezes properties against reassignment.
Like locking the labels on a sticker sheet.
Partial opens every property for small updates.
Readonly<T>
Readonly<User> makes every property readonly.
Readonly means you can read but not reassign.
Nested objects are not deeply frozen.
Deeply frozen means every nested bag is locked too.
Use recursive helpers if you need deep immutability.
readonly on arrays and tuples
ReadonlyArray<T> or readonly T[] blocks mutating methods.
Mutating means changing the list in place.
Readonly tuples lock both length meaning and element assignment.
Partial<T> for updates
When updating an entity, callers often send only the fields that changed.
An entity is one record, like one user.
Partial models that without inventing a second type by hand.
Combining Partial and Readonly
You can nest helpers: Readonly<Partial<T>> for an immutable draft.
Or Partial<Readonly<T>> depending on intent.
Compose so the resulting type matches how you use the value.
Compose means stack helpers together.
When to pick which
Use Readonly to protect shared data.
Use Partial for incomplete updates.
Together they model safe drafts and safe snapshots.
A snapshot is a frozen picture of data at one moment.
Use Readonly to protect shared data. Use Partial for incomplete updates.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.