From 1a2df0ee4b5bf613309e5f367daab8c9238d9a2e Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Sun, 18 Aug 2024 18:39:12 +0100 Subject: [PATCH] fix: Improve type output for narrowed interfaces (intersect members of unions instead) (#370) --- .changeset/wild-tables-serve.md | 5 +++++ src/selection.ts | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 .changeset/wild-tables-serve.md diff --git a/.changeset/wild-tables-serve.md b/.changeset/wild-tables-serve.md new file mode 100644 index 00000000..bcbd0a5f --- /dev/null +++ b/.changeset/wild-tables-serve.md @@ -0,0 +1,5 @@ +--- +"gql.tada": patch +--- + +Improve type output readability for interfaces with narrowed types diff --git a/src/selection.ts b/src/selection.ts index 06c61694..bb6ba9b5 100644 --- a/src/selection.ts +++ b/src/selection.ts @@ -12,6 +12,8 @@ type ObjectLikeType = { fields: { [key: string]: any }; }; +type narrowTypename = T extends { __typename?: Typename } ? T : never; + type unwrapTypeRec< Type, SelectionSet, @@ -95,7 +97,7 @@ type getFragmentSelection< ? Fragments[Node['name']['value']] extends { [$tada.ref]: any } ? Type extends { kind: 'INTERFACE'; name: any } ? /* This protects against various edge cases where users forget to select `__typename` (See `getSelection`) */ - Fragments[Node['name']['value']][$tada.ref] & { __typename?: PossibleType } + narrowTypename : Fragments[Node['name']['value']][$tada.ref] : getPossibleTypeSelectionRec< Fragments[Node['name']['value']]['selectionSet']['selections'], @@ -227,7 +229,9 @@ type getPossibleTypeSelectionRec< > : SelectionAcc > - : obj & SelectionAcc['rest']; + : SelectionAcc['rest'] extends infer T + ? obj + : never; type getOperationSelectionType< Definition,