Last active
July 1, 2025 15:34
-
-
Save fedetibaldo/61339ec52bbde2828feb65b3188569fc to your computer and use it in GitHub Desktop.
Map an object type to a union of object paths
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
/** | |
* 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