Created
August 9, 2019 03:58
-
-
Save Grohden/4e1a927f9c0ea018b7f5dbcf667205b2 to your computer and use it in GitHub Desktop.
Utility type to pick keys by type
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
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" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
god forgive me