Readonly<Type>
is
one of TypeScript's built in utility
types which:
Constructs a type with all properties of
Type
set toreadonly
, meaning the properties of the constructed type cannot be reassigned.
Use a mapped type [P in keyof T]: T[P]
that iterates over all the properties of the input type T
and replaces each property with a readonly
version of its type:
type MyReadonly<T> = {
readonly [P in keyof T]: T[P]
}
Using MyReadonly
, we can now rewrite the example from the challenge:
interface Todo {
title: string
description: string
}
const todo: MyReadonly<Todo> = {
title: 'Hey',
description: 'foobar',
}
todo.title = 'Hello' // Error: Cannot assign to 'title' because it is a read-only property
todo.description = 'barFoo' // Error: Cannot assign to 'description' because it is a read-only property
MyReadonly<Todo>
produces a new type that is equivalent to:
{
readonly title: string;
readonly description: string;
}