Skip to content

对象部分属性只读

470字约2分钟

2022-12-01

题目

Github: Readonly 2

实现一个泛型 MyReadonly2<T, K>,它带有两种类型的参数TK

K指定应设置为ReadonlyT的属性集。如果未提供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

解题思路

泛型参数K 需要约束为 T 的属性集,同时默认值为 T的属性集;

创建一个只含有 只读属性的对象类型,通过 in 操作符,遍历T 的所有成员,映射给泛型属性 P

创建一个只含有可读属性的对象类型, 通过 as 语法,重新映射给泛型属性 P

合并两个对象类型即可实现 MyReadonly<T, K>

答案

type MyReadonly2<T, K extends keyof T = keyof T> = {
  readonly [P in K]: T[P]
} & {
  [P in keyof T as P extends K ? never : P]: T[P]
}

验证

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
}

参考