Skip to content

Instantly share code, notes, and snippets.

@iohcidnal
Created June 23, 2022 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iohcidnal/ce4943939f8274a0bb6e9dd7377d246b to your computer and use it in GitHub Desktop.
Save iohcidnal/ce4943939f8274a0bb6e9dd7377d246b to your computer and use it in GitHub Desktop.
Create a new intersection type from an existing type
type Foo = {
prop1: {
value: string;
};
prop2: {
value: string[];
};
prop3: {
value: number;
};
prop4: {
value: number[];
};
};
/** Create a new intersection type from an existing type */
type IntersectProps<T, TKey extends keyof T> = {
[K in TKey]: (arg: T[K]) => void;
}[TKey] extends (arg: infer V) => void
? V
: never;
type Intersected = IntersectProps<Foo, keyof Foo>;
// ^?
/** Intersected is:
type Intersected = {
value: string;
} & {
value: string[];
} & {
value: number;
} & {
value: number[];
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment