Skip to content

Latest commit

 

History

History
36 lines (27 loc) · 1.17 KB

IsRequiredKey.md

File metadata and controls

36 lines (27 loc) · 1.17 KB
标题 标签
IsRequiredKey(是否是必选属性) extends,infer(继承,推断)

是否是必选属性。

  • 使用 Pick 类型根据属性提取属性值,如果与使用 Required 将提取的属性转成必选属性相同,则返回 true,否则返回 false。

代码如下:

type IsRequiredKey<T, U extends keyof T> = Pick<T, U> extends Required<
  Pick<T, U>
>
  ? true
  : false;

使用方式:

type IsRequiredKeyRes1 = IsRequiredKey<{ a: number; b?: string }, 'a'>; //  true
type IsRequiredKeyRes2 = IsRequiredKey<{ a: undefined; b: string }, 'a'>; //  true
type IsRequiredKeyRes3 = IsRequiredKey<{ a: number; b?: string }, 'b'>; //  false
type IsRequiredKeyRes4 = IsRequiredKey<{ a: number; b?: string }, 'b' | 'a'>; //  false
type IsRequiredKeyRes5 = IsRequiredKey<
  { a: undefined; b: undefined },
  'b' | 'a'
>; //  true

应用场景

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