Last active
April 1, 2022 13:51
-
-
Save jfet97/5b25f7642be1ac9cd4fe1441abc45e87 to your computer and use it in GitHub Desktop.
Use intersections to filter unions
This file contains 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 Decision = "yes" | "no" | 1 | 0 | true | false; | |
type JustStrings = Decision & string; | |
/// = ("yes" | "no" | 1 | 0 | true | false) & string | |
/// = | ("yes" & string) | ("no" & string) | |
/// | (1 & string) | (0 & string) | |
/// | (true & string)| (false & string)) | |
/// = "yes" | "no" | never | never | never | never | |
/// = "yes" | "no" | |
// | |
// when mapping types, if I want to extract the string keys I generally prefer & string to Extract | |
type GetMethods<T extends object> = { | |
[P in keyof T & string as `get${Capitalize<P>}`]: () => T[P]; | |
}; | |
type R = GetMethods<{ foo: string; 0: number }>; | |
// = { getFoo: () => string; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment