On this page
readonly
Prev: `as const` | Up: builtins | Next: `is`
Rel: builtins, Structural Typing & Narrowing
Ref: TypeScript Docs — readonly
Tags: #public #web #typescript #builtins #tier/core
Syntax & Overview
interface Point { readonly x: number; readonly y: number; }
Prevents compile-time reassignment of properties, array elements (readonly T[]), or tuples (readonly [number, number]).
Notes & Pitfalls
- Compile-time shallow immutability: Prevents accidental mutation (
readonly string[],readonly id: number), emitting zero runtime overhead. - Not deep: Nested objects inside a
readonlyproperty remain mutable unless markedreadonlytoo.
Example
interface Config {
readonly host: string;
readonly ports: readonly number[];
}
const cfg: Config = { host: "localhost", ports: [80, 443] };
// cfg.ports.push(8080); // compile-time error
Linked from
Related notes
- tuts/typescript/builtins/as_const Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/is Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins Linked here; links to this page; shared tags: #builtins, #typescript, #web
- tuts/typescript/type_system/structural_typing_and_narrowing Linked here; shared tags: #typescript, #web
- tuts/typescript/builtins/any shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/asserts shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/enum shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/infer shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/keyof shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/namespace shared tags: #builtins, #tier, #typescript, #web