Created
February 13, 2025 20:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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