8 - 对象部分属性只读
原创2025/4/29大约 1 分钟
8 - 对象部分属性只读
by Anthony Fu (@antfu) #中等 #readonly #object-keys
题目
实现一个泛型MyReadonly2<T, K>
,它带有两种类型的参数T
和K
。
类型 K
指定 T
中要被设置为只读 (readonly) 的属性。如果未提供K
,则应使所有属性都变为只读,就像普通的Readonly<T>
一样。
例如
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
在 Github 上查看:https://tsch.js.org/8/zh-CN
代码
/* _____________ 你的代码 _____________ */
type MyReadonly2<T, K extends keyof T = keyof T> = {
readonly [P in K]: T[P]
} & Omit<T, K>
测试用例
/* _____________ 测试用例 _____________ */
import type { Alike, Expect } from '@type-challenges/utils'
type cases = [
Expect<Alike<MyReadonly2<Todo1>, Readonly<Todo1>>>,
Expect<Alike<MyReadonly2<Todo1, 'title' | 'description'>, Expected>>,
Expect<Alike<MyReadonly2<Todo2, 'title' | 'description'>, Expected>>,
Expect<Alike<MyReadonly2<Todo2, 'description' >, Expected>>,
]
// @ts-expect-error
type error = MyReadonly2<Todo1, 'title' | 'invalid'>
interface Todo1 {
title: string
description?: string
completed: boolean
}
interface Todo2 {
readonly title: string
description?: string
completed: boolean
}
interface Expected {
readonly title: string
readonly description?: string
completed: boolean
}
相关链接
分享你的解答:https://tsch.js.org/8/answer/zh-CN
查看解答:https://tsch.js.org/8/solutions
更多题目:https://tsch.js.org/zh-CN