Skip to content

Instantly share code, notes, and snippets.

@magicspon
Last active January 22, 2026 16:16
Show Gist options
  • Select an option

  • Save magicspon/ad5b5d31523c87733d6d86ef7798cc8c to your computer and use it in GitHub Desktop.

Select an option

Save magicspon/ad5b5d31523c87733d6d86ef7798cc8c to your computer and use it in GitHub Desktop.
Payload DangerouslyExpandRelations
type HasObject<T> = T extends object ? (T extends null ? false : true) : false
// Remove string from union if it contains both string and object types
type RemoveStringIfHasObject<T> = string extends T
? true extends HasObject<T>
? Exclude<T, string>
: T
: T
// Recursive helper that handles arrays / readonly arrays and objects
export type DangerouslyExpandRelations<T> =
// readonly arrays / tuples (simplified): handle element type first
T extends readonly (infer U)[]
? DangerouslyExpandRelations<RemoveStringIfHasObject<U>>[]
: // normal arrays (redundant but explicit)
T extends (infer U)[]
? DangerouslyExpandRelations<RemoveStringIfHasObject<U>>[]
: // objects: map properties and recurse (also handles optional/nullable)
T extends object
? {
[K in keyof T]: DangerouslyExpandRelations<
RemoveStringIfHasObject<T[K]>
>
}
: // primitives / others
T