How to make a property of an object is read-only using typescript

Readonly<Type>

It will set all the properties of Type to read-only, which means the properties of the Type cannot be reassigned.

Let's see with an example so that we can understand easily

interface Person {
    name: string,
    age: number
}

let person: Readonly<Person> = {
    name: 'JK',
    age: 12
};

person.name = 'Jaya'; // it will give an error

The above line of code will throw an error like below

Cannot assign to the name because it is a read-only property.