i try to extend a React Component Class with generic Parameters but I cannot get it working. According to the TypeScript Docs and this TypeScript Class Generic Parameter Constraints, and this extending a class with a generic T I ended up in the following (not working) code:
Base Class:
class BaseView<P, S> extends React.Component<P, S> {
public constructor(props: P, state: S) {
super(props, state);
}
}
const mapStateToProps = store => {
console.log('store', store);
return {
store
};
};
function mapDispatchToProps(dispatch) {
console.log('dispatch', dispatch);
return {
};
}
export default connect(mapStateToProps, mapDispatchToProps)((BaseView as any));
export function subclass<P, S>(c): BaseView<P, S> {
return c;
}
The class which extends the Base Class:
I tried different ways, using the factory like this:
export class Documents extends subclass(BaseView)<DocumentsProperties, DocumentsState> {
// this leads into ""BaseView<{}, {}>" is no constructor function type
or without using the factory:
export class Documents extends BaseView<DocumentsProperties, DocumentsState> {
// no base constructor has the specified number of type arguments
I also tried out several other ways but i feel like i was programming for the first time of my life....I cannot get it running for multiple Parameters. What am I missing here?
greetings Messerbill
subclass()to returntypeof BaseViewand notBaseView<P,S>. I don't understand why the latter doesn't work, though. I can't reproduce your error in the Playground. Can you reproduce the error in a self-contained (no libraries) example? If not you'll have to wait for someone with react installed in their environmentcto be? I'm guessing you wantcto be some super constructor. Is it React?new c()which did not work either(c : { new() : BaseView<P,S> })for bothcand the return type.connectand generic components do not mix easily. See this answer for the approach I recommend. Let me know if you have trouble applying it to your scenario.