Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 1.26 KB

PartialByKeys.md

File metadata and controls

39 lines (29 loc) · 1.26 KB
标题 标签
PartialByKeys(根据属性让接口可选) extends,key(继承,属性)

根据属性让接口可选。

  • 创建一个 IntersectionToObj 类型用于将联合接口类型转换成接口,该类型就是重新读取接口 T 的属性,并重新赋属性值。(如: {a: number} & {b: string} -> {a: number, b: string})。
  • 将接口 T 的所有类型变成可选,与使用 Exclude 排除掉属于 U 的属性组合成的接口进行联合,最后使用 IntersectionToObj 将联合类型转成接口。

代码如下:

type IntersectionToObj<T> = {
  [K in keyof T]: T[K];
};
type PartialByKeys<T, U> = IntersectionToObj<
  {
    [P in keyof T]?: T[P];
  } & { [K in Exclude<keyof T, U>]: T[K] }
>;

使用方式:

interface PartialByKeysUser {
  name: string;
  age: number;
  address: string;
}

type PartialByKeysUserPartialName = PartialByKeys<PartialByKeysUser, 'name'>; // { name?:string; age:number; address:string }

应用场景

如下所示,鼠标悬浮到对应的类型变量可以查看类型。