Skip to content

Instantly share code, notes, and snippets.

@aocenas
Last active December 25, 2018 21:27
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 aocenas/5200570ece4358d0133cfcbdecfe5d53 to your computer and use it in GitHub Desktop.
Save aocenas/5200570ece4358d0133cfcbdecfe5d53 to your computer and use it in GitHub Desktop.
Connect HOC full
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