Last active
December 25, 2018 21:27
-
-
Save aocenas/5200570ece4358d0133cfcbdecfe5d53 to your computer and use it in GitHub Desktop.
Connect HOC full
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
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>> | |
export type GetProps<C> = C extends React.ComponentType<infer P> ? P : never | |
export type Matching<InjectedProps, DecorationTargetProps> = { | |
[P in keyof DecorationTargetProps]: P extends keyof InjectedProps | |
? InjectedProps[P] extends DecorationTargetProps[P] | |
? DecorationTargetProps[P] | |
: InjectedProps[P] | |
: DecorationTargetProps[P] | |
} | |
const connect = <MappedProps, OwnProps>( | |
mapProps: (ownProps: OwnProps) => MappedProps | |
) => <C extends React.ComponentType<Matching<MappedProps, GetProps<C>>>>( | |
WrappedComponent: C | |
): React.ComponentType<JSX.LibraryManagedAttributes<C, Omit<GetProps<C>, keyof MappedProps>> & OwnProps> => { | |
return (props: any) => { | |
const newProps = mapProps(props) | |
return <WrappedComponent {...props} {...newProps}/> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment