假设我有以下几种类型:
interface FooAction {
type: "foo";
data: any;
}
interface BarAction {
type: "bar";
data: any;
}
type AllActions = FooAction | BarAction;
给出AllActions
,如何基于type
属性生成联合类型?我想检索一个类型:
type AllTypes = "foo" | "bar"
答案 0 :(得分:3)
您可以使用index type query:
type AllTypes = AllActions['type'] // same as "foo" | "bar"
答案 1 :(得分:3)