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,