Created
September 26, 2019 00:47
-
-
Save itsMapleLeaf/2ad262f37fd964be55d1e5ac87ffac5f to your computer and use it in GitHub Desktop.
TypeScript - filter type keys by value
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 Thing = { | |
| a: number | |
| b: number | |
| c: string | |
| d: boolean | |
| } | |
| type Values<O> = O[keyof O] | |
| type KeysWithType<O, T> = Values<{ [K in keyof O]: O[K] extends T ? K : never }> | |
| type ThingNumberKeys = KeysWithType<Thing, number> // a | b | |
| function doThing(key: KeysWithType<Thing, number>, thing: Thing) { | |
| return thing[key] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment