结论:泛型的参数不一样
用例:
type User = {
id: string;
name: string;
email: string;
};
type UserWithoutEmail = Omit<User, "email">;// UserWithoutEmail ={id: string;name: string;}
与Omit作比较:
Omit左右两个参数属于不同类型,左是一个完整的类型,包含key、value
Exclude左右两个参数属于同种类型
用例:
type T0 = Exclude<"a" | "b" | "c", "a">;
// "b" | "c"
type T1 = Exclude<"a" | "b" | "c", "a" | "b">;
// "c"
type T2 = Exclude<string | number | (() => void), Function>;
// string | number
Omit和Exclude都是做属性剔除string、boolean等,而Omit的第二参数则必须是第一参数的子属性Pick搭配Exclude实现Omit:Exclude先剔除不要的键名,挑出想要的键名,Pick再从键值对中根据键名挑选出来。
实现公式:Omit=Pick