实现 Omit
不使用 Omit 实现 TypeScript 的 Omit<T, K>
泛型。
// Omit 会创建一个省略 K 中字段的 T 对象。
interface Todo {
title: string
description: string
completed: boolean
}
type TodoPreview = MyOmit<Todo, 'description' | 'title'>
const todo: TodoPreview = {
completed: false,
}
/**
* 思路:
* Omit 是为了忽略一些类型的 key 属性
*/
type MyOmit<T, K extends keyof T> = {
[Key in Exclude<keyof T, K>]: T[Key];
};
type MyOmit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;