将记录键类型提取为联合类型

Extract Record Key Types into Union Type

鉴于:

interface ActionA {
  type: 'AAAA';
  stuff: string[];
}

interface ActionB {
  type: 'BBBB';
  otherStuff: string[];
}

是否有类似的东西:

type ActionTypes = ExtractToUnion<ActionA | ActionB, "type">;

构造一个类型:

type ActionTypes = 'AAAA' | 'BBBB';

您可以使用 index access type 查找联合的 type 属性:

type ActionTypes = (ActionA | ActionB)["type"]; // "AAAA" | "BBBB"

Playground