type-challenges/questions/8-medium-readonly-2
2020-07-31 17:50:09 +08:00
..
2020-07-31 17:50:09 +08:00
2020-07-26 02:28:55 +08:00
2020-07-31 17:50:09 +08:00
2020-07-26 02:28:55 +08:00
2020-07-26 02:28:55 +08:00

Readonly 2 medium #readonly #object-keys

by Anthony Fu @antfu

Take the Challenge

Implement a generic MyReadonly2<T, K> which takes two type argument T and K.

K specify the set of properties if T that should set to Readonly. When K is not provided, it should make all properties readonly just like the normal Readonly<T>.

For example

interface Todo {
  title: string
  description: string
  completed: boolean
}

const todo: MyReadonly2<Todo, 'title' | 'description'> = {
  title: "Hey",
  description: "foobar",
  completed: false,
}

todo.title = "Hello" // Error: cannot reassign a readonly property
todo.description = "barFoo" // Error: cannot reassign a readonly property
todo.completed = true // OK

Back Share your Solutions Check out Solutions

Related Challenges

7・Readonly<T> 9・Deep Readonly