Optional property (prop?: Type) có thể có hoặc không, type là Type | undefined khi access.
Khác với prop: Type | undefined — cái sau vẫn yêu cầu key có mặt khi tạo object. readonly property không thể reassign sau khởi tạo (chỉ compile time). Readonly<T> làm tất cả properties readonly.
typescript
interface User {
id: number;
nickname?: string; // caller có thể bỏ qua
bio: string | undefined; // caller phải cung cấp key, nhưng value có thể undefined
readonly createdAt: Date; // không thể reassign
}Optional property (prop?: Type) may or may not be present; the type is Type | undefined when accessed.
Unlike prop: Type | undefined — the latter still requires the key to be present when constructing the object. readonly property cannot be reassigned after initialization (compile time only). Readonly<T> makes all properties readonly.
typescript
interface User {
id: number;
nickname?: string; // caller can omit it
bio: string | undefined; // caller must provide key, but value can be undefined
readonly createdAt: Date; // cannot be reassigned
}