Created
April 10, 2021 03:46
-
-
Save on3dd/6050a4673e96008c27f2175f0bdad466 to your computer and use it in GitHub Desktop.
typescript helper inferring type from union type by key
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 Helper<T, K extends string | number> = Extract<T, Record<K, unknown>>; |
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 A = { | |
a: string | |
qwe: string; | |
} | |
type B = { | |
b: string; | |
asd: string; | |
} | |
type C = { | |
c: string; | |
zxc: string; | |
} | |
type Res = Helper<A | B | C, "a">; | |
// type Res = { | |
// a: string; | |
// qwe: string; | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment