Skip to content

Instantly share code, notes, and snippets.

@maxhilliard
Created May 26, 2023 09:01
Show Gist options
  • Save maxhilliard/eea4c0e6513ac22a3e87d5c391d5f487 to your computer and use it in GitHub Desktop.
Save maxhilliard/eea4c0e6513ac22a3e87d5c391d5f487 to your computer and use it in GitHub Desktop.
TypeScript utility type to pick deep keys of an object
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