[Typescript Challenges] 2. Easy -- readonly

For 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
/* _____________ Your Code Here _____________ */

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


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<MyReadonly<Todo1>, Readonly<Todo1>>>,
]

interface Todo1 {
  title: string
  description: string
  completed: boolean
  meta: {
    author: string
  }
}
posted @ 2022-08-31 15:26  Zhentiw  阅读(26)  评论(0编辑  收藏  举报