Skip to content

Commit

Permalink
fix: Improve type output for narrowed interfaces (intersect members o…
Browse files Browse the repository at this point in the history
…f unions instead) (#370)
  • Loading branch information
kitten authored Aug 18, 2024
1 parent ca8b441 commit 1a2df0e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-tables-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gql.tada": patch
---

Improve type output readability for interfaces with narrowed types
8 changes: 6 additions & 2 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type ObjectLikeType = {
fields: { [key: string]: any };
};

type narrowTypename<T, Typename> = T extends { __typename?: Typename } ? T : never;

type unwrapTypeRec<
Type,
SelectionSet,
Expand Down Expand Up @@ -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], PossibleType>
: Fragments[Node['name']['value']][$tada.ref]
: getPossibleTypeSelectionRec<
Fragments[Node['name']['value']]['selectionSet']['selections'],
Expand Down Expand Up @@ -227,7 +229,9 @@ type getPossibleTypeSelectionRec<
>
: SelectionAcc
>
: obj<SelectionAcc['fields']> & SelectionAcc['rest'];
: SelectionAcc['rest'] extends infer T
? obj<SelectionAcc['fields'] & T>
: never;

type getOperationSelectionType<
Definition,
Expand Down

0 comments on commit 1a2df0e

Please sign in to comment.