In flow there is support for $Compose
functions (see recompose as example). However, I can't seem to find such a mechanism in typescript. it seems that the best typescript can do is something like https://github.com/reactjs/redux/blob/master/index.d.ts#L416-L460. What's the equivalent to $Compose
in Typescript?
EDIT: What I'm trying to accomplish is to type the compose
function from recompose
or redux
such that it's typesafe. In particular, with react higher order components, I want to ensure that the output props of one HOC satisfy the input props of the next HOC. This is my current workaround and seems to work reasonably well - though I was hoping there is a good way to do this natively in typescript.
/** Wraps recompose.compose in a type-safe way */
function composeHOCs<OProps, I1, IProps>(
f1: InferableComponentEnhancerWithProps<I1, OProps>,
f2: InferableComponentEnhancerWithProps<IProps, I1>,
): ComponentEnhancer<IProps, OProps>
function composeHOCs<OProps, I1, I2, IProps>(
f1: InferableComponentEnhancerWithProps<I1, OProps>,
f2: InferableComponentEnhancerWithProps<I2, I1>,
f3: InferableComponentEnhancerWithProps<IProps, I2>,
): ComponentEnhancer<IProps, OProps>
function composeHOCs<OProps, I1, I2, I3, IProps>(
f1: InferableComponentEnhancerWithProps<I1, OProps>,
f2: InferableComponentEnhancerWithProps<I2, I1>,
f3: InferableComponentEnhancerWithProps<I3, I2>,
f4: InferableComponentEnhancerWithProps<IProps, I3>,
): ComponentEnhancer<IProps, OProps>
function composeHOCs(
...fns: Array<InferableComponentEnhancerWithProps<any, any>>
): ComponentEnhancer<any, any> {
return compose(...fns)
}
$Compose
is undocumented and your link isn't clear. Can you provide a more detailed explanation (perhaps with examples) of what it does / what you want to do? – Provocation