Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Created July 21, 2020 18:40
Show Gist options
  • Save karol-majewski/ce726e3dd26a208de7545189a6285fe8 to your computer and use it in GitHub Desktop.
Save karol-majewski/ce726e3dd26a208de7545189a6285fe8 to your computer and use it in GitHub Desktop.
Pick properties from a TypeScript type by their value
interface Dog {
name: string;
bark(): void;
}
declare global {
namespace Pick {
type ByValue<T, U> = Pick<T, PropertyOfValue<T, U>>
}
}
type PropertyOfValue<T, V> = {
[K in keyof T]-?: T[K] extends V
? K
: never
}[keyof T];
type Methods = Pick.ByValue<Dog, Function> // { bark(): void }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment