Skip to content

Instantly share code, notes, and snippets.

@ssr765
Created February 13, 2025 20:04
Show Gist options
  • Select an option

  • Save ssr765/8135ad85ee763a16f63bf7278dc0a99c to your computer and use it in GitHub Desktop.

Select an option

Save ssr765/8135ad85ee763a16f63bf7278dc0a99c to your computer and use it in GitHub Desktop.
My very first TypeScript complex type. It "loads" relations from models retrieved from a Laravel API making the properties not null. It does recursively, so can work with deep relationships.
export type With<Model, Relation extends string> =
Relation extends `${infer First}.${infer Rest}`
? Omit<Model, First> & {[K in First]: With<NonNullable<Model[First]>, Rest>}
: Relation extends keyof Model
? Omit<Model, Relation> & Required<Pick<Model, Relation>>
: never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment