Skip to content

Commit

Permalink
fix(types): handle union types better
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Oct 2, 2024
1 parent 8ef2ab3 commit 914aa80
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/lib/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ type NodeFields<T extends object> = T extends any
: never
: never

// Utility types for handling cases where multiple object types are possible
type Keys<T> = T extends object ? keyof T : never
type Access<T, K> = T extends object
? K extends keyof T
? T[K]
: undefined
: undefined

/**
* The return type of the `select` function. It takes an object and a
* dot-separated field path. The field path should *not* include node types
Expand All @@ -24,13 +32,13 @@ export type FieldSelection<
> = T extends any
? NodeFields<T> extends infer TFields
? TFieldPath extends `${infer TField}.${infer TRest}`
? TField extends keyof TFields
? TFields[TField] extends object
? FieldSelection<TFields[TField], TRest>
? TField extends Keys<TFields>
? Access<TFields, TField> extends object
? FieldSelection<Access<TFields, TField>, TRest>
: undefined
: undefined
: TFieldPath extends keyof TFields
? TFields[TFieldPath]
: TFieldPath extends Keys<TFields>
? Access<TFields, TFieldPath>
: undefined
: never
: never
Expand Down

0 comments on commit 914aa80

Please sign in to comment.