Skip to content

Instantly share code, notes, and snippets.

@fedetibaldo
Last active July 1, 2025 15:34
Show Gist options
  • Save fedetibaldo/61339ec52bbde2828feb65b3188569fc to your computer and use it in GitHub Desktop.
Save fedetibaldo/61339ec52bbde2828feb65b3188569fc to your computer and use it in GitHub Desktop.
Map an object type to a union of object paths
/**
* Map an object type to a union of object paths
* @example
* ```
* type Foo = { foo: { bar: number, baz: number[] } };
* type FooPath = ObjectPath<Foo>;
* // ^ "foo" | "foo.bar" | "foo.baz"
* ```
*/
export type ObjectPath<T> = {
[K in keyof T]:
K extends string
? (
T[K] extends object
? (
T[K] extends Array<infer U>
? K // | `${K}.${number}`
: K | `${K}.${ObjectPath<T[K]>}`
)
: K
)
: never;
}[keyof T];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment