Skip to content

Instantly share code, notes, and snippets.

@cjfswd
Last active April 14, 2023 12:01
Show Gist options
  • Save cjfswd/b60ef255f7d532df5195239949700213 to your computer and use it in GitHub Desktop.
Save cjfswd/b60ef255f7d532df5195239949700213 to your computer and use it in GitHub Desktop.
type to path into object keys with typescript,
type PathInto<T extends Record<string, any>> = keyof {
[K in keyof T as T[K] extends string
? K
: T[K] extends Record<string, any>
? `${K & string}.${PathInto<T[K]> & string}`
: never]: any
}
type PathTrace<T, K extends keyof T = keyof T> =
K extends string
? T[K] extends Record<string, any>
? `${K}.${Path<T[K]>}`
: K
: never;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment