TypeScript · Types and shapes

Readonly

Prevents reassignment of the property after creation. It is shallow and checked only by the compiler.

Syntax
readonly id: string

Example

Example
type User = { readonly id: string; name: string };
const user: User = { id: "u1", name: "Ada" };
// user.id = "u2";

More in this group

any Array types enums Function type Interface Literal type never null and undefined

Learn this in the course

Reference pages are the quick answer. The course explains the why, shows the full example and ends with a challenge.

Open the course