Created
December 2, 2024 17:29
-
-
Save zayniddindev/4d4b76f5ae68d014b34000822e46d558 to your computer and use it in GitHub Desktop.
Recursively extracts all possible dot-separated key paths from a nested object
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
| /** | |
| * Recursively extracts all possible dot-separated key paths from a nested object. | |
| * If the object has nested objects, it concatenates parent keys with nested keys using dot notation. | |
| * | |
| * @template T - The object type to extract keys from. | |
| * | |
| * @example | |
| * type Obj = { | |
| * campaigns: { | |
| * list: {}; | |
| * get: {}; | |
| * }; | |
| * orders: { | |
| * list: {}; | |
| * }; | |
| * }; | |
| * | |
| * // Extracted keys: "campaigns" | "campaigns.list" | "campaigns.get" | "orders" | "orders.list" | |
| * type Keys = ExtractKeys<Obj>; | |
| */ | |
| export type ExtractKeys<T> = T extends object | |
| ? { | |
| [K in keyof T & string]: K | (T[K] extends object ? `${K}.${ExtractKeys<T[K]>}` : K); | |
| }[keyof T & string] | |
| : never; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment