type-challenges (初級)
4. Pick
Utility Typeの1つであるPick
を自前で実装する。
https://github.com/type-challenges/type-challenges/blob/main/questions/00004-easy-pick/README.ja.md
解答例
interface Todo { title: string description: string completed: boolean}
type MyPick<T, K extends keyof T = keyof T> = { [key in K]: T[key]}
type TodoPreview = MyPick<Todo, 'title' | 'completed'>
const todo: TodoPreview = { title: 'Clean room', completed: false}
7. Readonly
interface Todo { title: string description: string}
type MyReadonly<T> = { readonly [key in keyof T]: T[key]}
const todo: MyReadonly<Todo> = { title: 'Hey', description: 'foobar'}
todo.title = 'Hello' // Error: cannot reassign a readonly propertytodo.description = 'barFoo' // Error: cannot reassign a readonly property