Created
January 14, 2022 12:45
-
-
Save IniZio/09c91ab89fe5191fba3af63a1c389884 to your computer and use it in GitHub Desktop.
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
import { ComponentType } from "react"; | |
type GetProps<C> = C extends ComponentType<infer P> ? P : never; | |
type Matching<InjectedProps, DecorationTargetProps> = { | |
[P in keyof DecorationTargetProps]: P extends keyof InjectedProps | |
? InjectedProps[P] extends DecorationTargetProps[P] | |
? DecorationTargetProps[P] | |
: InjectedProps[P] | |
: DecorationTargetProps[P]; | |
}; | |
const hoc = | |
<MappedProps, OwnProps>(mapProps: (ownProps: OwnProps) => MappedProps) => | |
<C extends ComponentType<Matching<MappedProps, GetProps<C>>>>( | |
WrappedComponent: C | |
): ComponentType< | |
JSX.LibraryManagedAttributes<C, Omit<GetProps<C>, keyof MappedProps>> & | |
OwnProps | |
> => { | |
return (props: any) => { | |
const newProps = mapProps(props); | |
return <WrappedComponent {...props} {...newProps} />; | |
}; | |
}; | |
export default hoc; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment