Skip to content

Instantly share code, notes, and snippets.

@Grohden
Created August 9, 2019 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grohden/4e1a927f9c0ea018b7f5dbcf667205b2 to your computer and use it in GitHub Desktop.
Save Grohden/4e1a927f9c0ea018b7f5dbcf667205b2 to your computer and use it in GitHub Desktop.
Utility type to pick keys by type
export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
export type PickKeysDeep<T, P = any> = {
[K in keyof T]: T[K] extends object
? P extends Primitive
? PickKeysDeep<T[K], P>
: K | PickKeysDeep<T[K], P>
: P extends Primitive
? P extends T[K]
? K
: never
: never
}[keyof T]
type Test = PickKeysDeep<{
a: number,
test: {
b: {
c: number,
y: string
}
c: string
}
}, object>
const a: Test = "a"
@Grohden
Copy link
Author

Grohden commented Aug 9, 2019

god forgive me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment