Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 1.16 KB

notes.md

File metadata and controls

43 lines (32 loc) · 1.16 KB

Readonly

Readonly<Type> is one of TypeScript's built in utility types which:

Constructs a type with all properties of Type set to readonly, meaning the properties of the constructed type cannot be reassigned.

Solution

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;
}