The Readonly utility type helps you make an object's properties unchangeable. This means you can protect data from being accidentally changed.
When you want to make sure an object's data stays the same after creation.
When sharing data between parts of a program and you want to avoid accidental changes.
When working with configuration objects that should not be modified.
When you want to clearly show that some data is meant to be constant.
When you want to catch errors early by preventing property changes in your code.
Syntax
Typescript
Readonly<Type>
Type is the object type you want to make read-only.
The result is a new type where all properties are readonly.
Examples
This creates a user object whose name and age cannot be changed.
Typescript
type User = {
name: string;
age: number;
};
const user: Readonly<User> = {
name: "Alice",
age: 30
};
Trying to change point.x will cause a TypeScript error because it is readonly.
Typescript
type Point = {
x: number;
y: number;
};
const point: Readonly<Point> = { x: 10, y: 20 };
// point.x = 15; // Error: Cannot assign to 'x' because it is a read-only property.
Sample Program
This program creates a readonly myBook object and prints its title. Trying to change the title will cause an error.
Typescript
type Book = {
title: string;
author: string;
};
const myBook: Readonly<Book> = {
title: "Learn TypeScript",
author: "Jane Doe"
};
console.log(myBook.title);
// Uncommenting the next line will cause a TypeScript error// myBook.title = "New Title";
OutputSuccess
Important Notes
Readonly only affects the top-level properties. Nested objects inside are not automatically readonly.
Use Readonly to help prevent bugs by making data immutable.
Summary
Readonly makes all properties of an object type unchangeable.
It helps protect data from accidental changes.
It is useful for constants, configs, and safer code.
Practice
(1/5)
1. What does the Readonly utility type do in TypeScript?
easy
A. Makes all properties optional
B. Allows properties to be changed only once
C. Removes all properties from an object type
D. Makes all properties of an object type unchangeable
Solution
Step 1: Understand the purpose of Readonly
The Readonly utility type is designed to make all properties of an object type immutable, meaning they cannot be changed after initialization.
Step 2: Compare with other options
Options B, C, and D describe behaviors that are not related to Readonly. Only Makes all properties of an object type unchangeable correctly describes making properties unchangeable.
Final Answer:
Makes all properties of an object type unchangeable -> Option D
Quick Check:
Readonly = makes properties unchangeable [OK]
Hint: Readonly means no changes allowed to object properties [OK]
Common Mistakes:
Thinking Readonly allows one-time changes
Confusing Readonly with Partial or Required types
Believing Readonly removes properties
2. Which of the following is the correct syntax to create a readonly type from an interface User?
easy
A. type ReadonlyUser = Readonly<User>();
B. type ReadonlyUser = Readonly<User>;
C. type ReadonlyUser = readonly User;
D. type ReadonlyUser = Readonly<User>[];
Solution
Step 1: Recall the syntax of Readonly utility
The correct syntax to create a readonly version of a type or interface is Readonly<TypeName> without parentheses or array brackets.
Step 2: Analyze each option
type ReadonlyUser = Readonly<User>; uses the correct generic syntax. type ReadonlyUser = Readonly<User>(); incorrectly uses parentheses. type ReadonlyUser = readonly User; uses lowercase 'readonly' which is a keyword for arrays or tuples, not for types. type ReadonlyUser = Readonly<User>[]; creates an array of readonly users, which is different.
Final Answer:
type ReadonlyUser = Readonly<User>; -> Option B
Quick Check:
Readonly syntax = Readonly<Type> [OK]
Hint: Use Readonly<Type> without parentheses or brackets [OK]
Common Mistakes:
Adding parentheses after Readonly
Using lowercase 'readonly' instead of utility type
Confusing array of readonly with readonly type
3. What will be the output of the following TypeScript code?