Sub topic 5
Required and Readonly
Prerequisites
This chapter assumes that you know TypeScript interfaces, optional properties marked with ?, type aliases, and generic syntax such as Array<T>. It also assumes that you understand that TypeScript checks types during development and compilation.
Required: removing optional properties
An optional property may be absent from an object:
interface Profile {
id: number;
name: string;
nickname?: string;
}
const shortProfile: Profile = {
id: 1,
name: "Ada",
};Required<T> is a utility type that creates a new type where every property in T is required. It removes the optional ? modifier.
interface Profile {
id: number;
name: string;
nickname?: string;
}
type CompleteProfile = Required<Profile>;
const profile: CompleteProfile = {
id: 1,
name: "Ada",
nickname: "Countess",
};
function saveProfile(value: CompleteProfile): void {
console.log(`Saving ${value.name} (${value.nickname})`);
}
saveProfile(profile);The profile object must contain id, name, and nickname. This is useful at a boundary where incomplete data has already been validated. For example, a form may collect a partial profile, but a database function may accept only a complete profile.
Required<T> changes only the top-level properties. If a property contains a nested object, that nested object is not recursively changed.
Readonly: preventing reassignment
Readonly<T> creates a type whose properties cannot be reassigned through that type.
interface Profile {
id: number;
name: string;
nickname?: string;
}
type DisplayProfile = Readonly<Profile>;
const displayProfile: DisplayProfile = {
id: 1,
name: "Ada",
};
console.log(displayProfile.name);
// @ts-expect-error: Cannot assign to 'name' because it is read-only
displayProfile.name = "Grace";Readonly<T> is useful when a function should inspect an object without changing its properties:
function printProfile(profile: Readonly<Profile>): void {
console.log(`${profile.id}: ${profile.name}`);
}
const profile: Profile = {
id: 1,
name: "Ada",
};
printProfile(profile);The type prevents accidental assignment inside printProfile. It does not copy the object and does not freeze it at runtime. JavaScript code can still change the object through another reference.
Choosing between them
| Utility type | Main effect | Typical use |
|---|---|---|
| `Required<T>` | Makes all top-level properties mandatory | Validated or completed data |
| `Readonly<T>` | Blocks property reassignment through the type | Read-only function inputs and constants |
These utilities can also be composed. For example, Readonly<Required<Profile>> describes a complete profile that callers cannot reassign through that value.
Shallow behavior and arrays
Both utilities affect only the outer object. Consider this type:
interface Account {
settings: {
theme: string;
};
}
type SafeAccount = Readonly<Account>;
const account: SafeAccount = {
settings: { theme: "light" },
};
// The outer property cannot be replaced.
// @ts-expect-error: Cannot assign to 'settings'
account.settings = { theme: "dark" };
// The nested property is still writable.
account.settings.theme = "dark";If nested values must also be read-only, use a recursive custom type or explicitly use Readonly at each level. For arrays, use readonly string[] or ReadonlyArray<string> to prevent operations that change the array, such as push.
Exercise
What type should you use for a function that accepts a complete User object but must not reassign any of its properties?
Show answer
Use Readonly<Required<User>>.