Created
April 8, 2025 04:31
-
-
Save A-Maged/37597e0b24c45267c4451d5e4538da15 to your computer and use it in GitHub Desktop.
Generate Dot-Notation Key Paths for Nested Types in TypeScript
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 Increment<N extends number, Arr extends any[] = []> = Arr['length'] extends N | |
? [...Arr, any]['length'] | |
: Increment<N, [...Arr, any]>; | |
type FilteredKeys<T> = Exclude<keyof T, keyof any[]>; | |
type DeepKeysPrefix<T, K extends keyof T, MaxDepth extends number, Depth extends number> = Depth extends MaxDepth | |
? never | |
: K extends string | number | |
? `${K}.${DeepKeys<T[K], MaxDepth, Increment<Depth>> & string}` | |
: never; | |
export type DeepKeys<T, MaxDepth extends number = 4, Depth extends number = 0> = Depth extends MaxDepth | |
? never | |
: T extends Record<string | number, any> | |
? FilteredKeys<T> extends infer K | |
? K extends string | number | |
? K | DeepKeysPrefix<T, K, MaxDepth, Increment<Depth>> | |
: never | |
: never | |
: never; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment