我正在使用graphql-code-generator从我的GraphQL查询中生成TypeScript定义,当前正在尝试从数组中提取特定的并集。在TypeScript中可以吗?我发现了an example where someone extracted a type from a generic并尝试使用Extract
函数,但这无法正常工作,仅返回never
:
export type Foo = Array<(
{
id: string;
__typename: 'Data1'
}
| {
id: string;
__typename: 'Data2'
}
)>;
type MyQueryData1 = Extract<Foo, { __typename: "Data1"}>
type MyQueryData2 = Extract<Foo, { __typename: "Data2"}>
答案 0 :(得分:1)
这是您想要的结果吗? ts playground
如果是这样,则必须从数组的元素值中从合并的联合类型中提取类型,
type MyQueryData1 = Extract<Foo[number], { __typename: "Data1"}>
type MyQueryData2 = Extract<Foo[number], { __typename: "Data2"}>