xieyezi/website-2022

typescript 练习-实现 Readonly

Opened this issue · 0 comments

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

example:

interface Todo {
  title: string
  description: string
}

const todo: MyReadonly<Todo> = {
  title: "Hey",
  description: "foobar"
}

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