Last active
June 12, 2023 12:32
-
-
Save ebeloded/c7cf37c5bde3e07b850dcabf78161623 to your computer and use it in GitHub Desktop.
NestedPartal.ts
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
type Primitive = string | number | boolean | undefined | null | |
type UnionToIntersection<U> = ( | |
U extends unknown ? (k: U) => void : never | |
) extends (k: infer I) => void | |
? I | |
: never | |
type AddPrefixToKeys< | |
Prefix extends string, | |
T extends Record<string, unknown> | |
> = { | |
[K in keyof T & string as `${Prefix}.${K}`]+?: T[K] | |
} | |
type NestedUpdateFields<T extends Record<string, unknown>> = | |
UnionToIntersection< | |
{ | |
[K in keyof T & string]: ChildUpdateFields<K, T[K]> | |
}[keyof T & string] | |
> | |
type ChildUpdateFields<K extends string, V> = V extends Record<string, unknown> | |
? AddPrefixToKeys<K, NestedPartial<V>> | |
: never | |
export type NestedPartial<T> = T extends Primitive | |
? T | |
: T extends {} | |
? { [K in keyof T]?: NestedPartial<T[K]> } & NestedUpdateFields<T> | |
: never |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment