Created
May 26, 2023 09:01
-
-
Save maxhilliard/eea4c0e6513ac22a3e87d5c391d5f487 to your computer and use it in GitHub Desktop.
TypeScript utility type to pick deep keys of an 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
type DeepObjectKeys<T> = T extends Record<string, unknown> | |
? { | |
[K in keyof T]-?: K extends string ? `${K}` | `${K}.${DeepObjectKeys<T[K]>}` : never | |
}[keyof T] | |
: never | |
const testObj = { | |
foo: { | |
bar: false, | |
baz: { | |
ban: 123, | |
}, | |
}, | |
awd: [true, false], | |
} | |
const _deepKey: DeepObjectKeys<typeof testObj> = 'foo.baz.ban' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment