Last active
April 14, 2023 12:01
-
-
Save cjfswd/b60ef255f7d532df5195239949700213 to your computer and use it in GitHub Desktop.
type to path into object keys with typescript,
This file contains 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 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