zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Typescript Challenges] 2. Easy -- readonly

typescript -- Easy readonly
2023-09-14 08:59:12 时间

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