Skip to content

Instantly share code, notes, and snippets.

@michal-wrzosek
Last active August 5, 2022 15:45
Show Gist options
  • Save michal-wrzosek/4d7a4fa4652f3b9cfdd09565d201cecb to your computer and use it in GitHub Desktop.
Save michal-wrzosek/4d7a4fa4652f3b9cfdd09565d201cecb to your computer and use it in GitHub Desktop.
Typing compose with multiple React HOCs (Typescript, HOC, React)
import { compose } from 'ramda';
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
type ABCProps = {
a: string;
b: string;
c: string;
};
const ABC: React.FC<ABCProps> = ({ a, b, c, children }) => (
<div>
<div>
{a} {b} {c}
</div>
<div>{children}</div>
</div>
);
const a = <P extends object>(Component: React.FC<P>): React.FC<Omit<P, 'a'>> => props =>
Component({ ...props, a: 'A' } as P);
const b = <P extends object>(Component: React.FC<P>): React.FC<Omit<P, 'b'>> => props =>
Component({ ...props, b: 'B' } as P);
const c = <P extends object>(Component: React.FC<P>): React.FC<Omit<P, 'c'>> => props =>
Component({ ...props, c: 'C' } as P);
type funcAll = React.FC<ABCProps>;
type funcNoA = React.FC<Omit<ABCProps, 'a'>>;
type funcNoAB = React.FC<Omit<ABCProps, 'a' | 'b'>>;
type funcNoABC = React.FC<Omit<ABCProps, 'a' | 'b' | 'c'>>;
const Test = compose<funcAll, funcNoA, funcNoAB, funcNoABC>(
c,
b,
a,
)(ABC);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment