Created
June 23, 2022 01:47
-
-
Save iohcidnal/ce4943939f8274a0bb6e9dd7377d246b to your computer and use it in GitHub Desktop.
Create a new intersection type from an existing type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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