interface Person {
    name: string;
    age: number;
}

interface ReadonlyPerson {
    readonly name: string;
    readonly age: number;
}

const person: ReadonlyPerson {
    name: "Wan"
    age: 28
}

This works.

 

There is a better way:

interface Person {
    name: string;
    age: number;
}

const person:Readonly<Person> {
    name: "Wan"
    age: 28
}

What it does, is mapping over each prop in Person and add readonly midifier to it.

 

We can write one by our own:

type MyReadonly<T> = {
    readonly [P in keyof T]: T[P]
}

 

freeze funtion:

function freezePerson<T> (obj: T): Readonly<T> {
    return Object.freeze(obj);
}