Readonly


Problem - On Github

The aim is to build the inbuilt Readonly utility type.

Out initial type given by the puzzle creator is

type MyReadonly<T> = any

Solution

If we think about what needs to happen this time is we take all the keys of a given object, and make those keys readonly.

So our type should:

  1. be an object - { ... }
  2. have its keys be read only - { readonly [... ] ...}
  3. and have the keys and values from the T object - [Key in keyof T]

So the answer is:

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


What to read more? Check out more posts below!