Skip to content

Instantly share code, notes, and snippets.

@dlaub3
Last active February 11, 2023 16:58
Show Gist options
  • Save dlaub3/823365161ae6e8437a967611682e573f to your computer and use it in GitHub Desktop.
Save dlaub3/823365161ae6e8437a967611682e573f to your computer and use it in GitHub Desktop.
TS Utilities
// Extract the desired values from a union of objects
type ExtractUnionValues<T extends {}, ExcludProps extends string = never> = T extends Extract<T, { [K in infer P]: any }> ? T[Exclude<P, ExcludProps>] : never
type PickUnionValues<T extends {}, Props extends string = never> = T extends Extract<T, { [K in infer P]: any }> ? T[Extract<P, Props>] : never
// alternative implementation
type ExtractUnionValues<T extends {}, ExcludedProps extends string = never> = T extends {} ? Omit<T, ExcludedProps>[keyof Omit<T, ExcludedProps>]: never
type PickUnionValues<T extends {}, Props extends string = never> = T extends {} ? Pick<T, Extract<keyof T, Props>>[keyof Pick<T, Extract<keyof T, Props>>]: never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment