mirror of
https://github.com/type-challenges/type-challenges.git
synced 2026-02-01 15:47:22 +00:00
2.8 KiB
2.8 KiB
Readonly 2

by Anthony Fu @antfu
2つの型引数TとKを取るMyReadonly2<T, K>を実装します。
Kが指定されている場合は、Tの中のKのプロパティのみを読み取り専用にします。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