Skip to content

Instantly share code, notes, and snippets.

@mvf4z7
Created November 4, 2020 21:53
Show Gist options
  • Save mvf4z7/0f6f2915c6f71d3c36baa9dab5d1558d to your computer and use it in GitHub Desktop.
Save mvf4z7/0f6f2915c6f71d3c36baa9dab5d1558d to your computer and use it in GitHub Desktop.
export type DiscriminateUnion<
T,
K extends keyof T,
V extends T[K]
> = T extends Record<K, V> ? T : never;
export type MapDiscriminatedUnion<
T extends Record<K, string>,
K extends keyof T
> = {
[V in T[K]]: DiscriminateUnion<T, K, V>;
};
type T1 = {
name: 'T1';
t1foo: true;
t1bar: 1;
};
type T2 = {
name: 'T2';
t2foo: [1, 2];
t2bar: 'hello world';
};
type T3 = {
name: 'T3';
t3foo: number[];
t3bar: 'hello world' | false;
};
type Plugins = T1 | T2 | T3;
export type test = DiscriminateUnion<Plugins, 'name', 'T2'>;
export type testmap = MapDiscriminatedUnion<Plugins, 'name'>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment