TS: assign function to `const` with generic type

Viewed 1470

In pre TS world, we used to assign simple HOC to const like this. (code uses recompose/mapProps)

const withAverage = mapProps(
    (props) => ({ ...props, average: avg(props.numbers) })
)

in TS world the code looks like this (it's not 100% accurate, but illustrate the problem)

const withAverage = <R extends DataRow, P extends WithData<R>>(
  component: React.ComponentType<P>
): React.ComponentType<P & WithAverage<R>> =>
  mapProps<P, P & WithAverage<R>>(
    (props) => ({ ...props, average: avg(props.numbers) })
(component);

the TypeScript version wraps HOC function in another HOC function, but it is not necessary, mapProps already provides HOC so just

const withAverage = mapProps...

is sufficient, no need to wrap it in another function like

const withAverage = Component => mapProps(...)(Component)

but AFAIK, there is no way how to assign HOC to const with support for generics, something like

const withAverage<I,O> = mapProps<I, O>(...)

Generics seem to be only supported for function declaration, not for HOC assignment. Or Am I wrong?

0 Answers
Related