Skip to content

Instantly share code, notes, and snippets.

@zayniddindev
Created December 2, 2024 17:29
Show Gist options
  • Select an option

  • Save zayniddindev/4d4b76f5ae68d014b34000822e46d558 to your computer and use it in GitHub Desktop.

Select an option

Save zayniddindev/4d4b76f5ae68d014b34000822e46d558 to your computer and use it in GitHub Desktop.
Recursively extracts all possible dot-separated key paths from a nested object
/**
* 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