I'm trying to connect a React element with react-redux that has arbitrary/generic props, but it's not compiling correctly. I've tried using JSXElementConstructor and (new (props: Props) => React.Component<Props, any>) instead of ComponentType, but got a different error about how Props could be instantiated with a different type. Here's the code. Any help would be appreciated.
https://github.com/piotrwitek/react-redux-typescript-guide/issues/55 looks related, but that seems to be about the props having a generic, instead of the props themselves being a generic.
Code
import React from "react";
import { connect } from "react-redux";
interface StoreState {
someState: number;
}
// Tried `React.JSXElementConstructor<Pros>`
// and `(new (props: Props) => React.Component<Props, any>)`
// instead of `React.ComponentType<Props>`, but got a different error.
function createWrapperComponent<Props>(
componentElement: React.ReactElement<
Props,
React.ComponentType<Props> & { update(props: Props): void }
>
) {
const props = componentElement.props;
const UnconnectedComponent = componentElement.type;
const ConnectedComponent = connect<StoreState, {}, Props, StoreState>(
(state) => state
)(UnconnectedComponent);
return class WrapperComponent extends React.Component<any> {
static update = UnconnectedComponent.update;
render() {
return <ConnectedComponent {...props} />;
}
};
}
Playground Link: Provided