ts不错的学习文档
实现pick
挑战
从对象中选出指定对象key的类型
type MyPick<T, K extends keyof T> = {
[key in K]:T[key]
}
实现只读
type MyReadonly<T> = {
readonly [key in keyof T]:T[key]
}
返回函数类型
type MyReturnType<T extends Function> =
T extends (...args: any) => infer R
? R
: never
omit去除指定键类型
interface Todo {
title: string
description: string
completed: boolean
}
type TodoPreview = MyOmit<Todo, 'description' | 'title'>
const todo: TodoPreview = {
completed: false,
}
type MyOmit<T,K extends keyof T> = {
[P in keyof T as P extends K ? never: P] :T[P]
}