Skip to content

Instantly share code, notes, and snippets.

@on3dd
Created April 10, 2021 03:46
Show Gist options
  • Save on3dd/6050a4673e96008c27f2175f0bdad466 to your computer and use it in GitHub Desktop.
Save on3dd/6050a4673e96008c27f2175f0bdad466 to your computer and use it in GitHub Desktop.
typescript helper inferring type from union type by key
type Helper<T, K extends string | number> = Extract<T, Record<K, unknown>>;
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