These property modifiers control whether a property is required and whether it can be reassigned.
Optional (?)
ts
{
: ;
?: ;
}
: = { : };
: = { : , : };
() {
u..;
u.?.;
}
These property modifiers control whether a property is required and whether it can be reassigned.
?) {
: ;
?: ;
}
: = { : };
: = { : , : };
() {
u..;
u.?.;
}
An optional property has type T | undefined, so the compiler forces you to handle the absent case — preventing "cannot read property of undefined" bugs.
interface Point { readonly x: number; readonly y: number; }
const p: Point = { x: 1, y: 2 };
p.x = 5; // ❌ Error: cannot assign to readonly property
// arrays too
const nums: readonly number[] = [1, 2, 3];
nums.push(4); // ❌ push doesn't exist on readonly array
readonly is compile-time only (no runtime enforcement), but it documents and enforces immutability in the type system, catching accidental mutations.
interface Config { readonly id: string; tags?: readonly string[]; }
type Frozen = Readonly<User>; // utility type makes ALL props readonly
? models genuinely optional data and forces null-safety; readonly expresses and enforces immutability (great for config, props, and preventing accidental state mutation).
Both push correctness checking into the compiler.