1

I created a custom component by inheriting React's Component as follows:

import React, { Component as ReactComponent, ReactElement } from "react";
import { Button as ReactButton, View } from "react-native";

interface IMyComponentProps extends React.Props<IMyComponentProps> {
  colWidth?: number;
}

class MyComponent extends ReactComponent<IMyComponentProps> {
  getProps() { //dummy function to check if subClass can access this function
    return this.props;
  }

  renderChildren() { //adding custom props to child MyComponents
    return React.Children.map(this.props.children, (child: ReactElement<any>) => {
      const isMyComponent: boolean =
        child && //sometimes child is undefined
        child.type &&
        typeof child.type !== "string" &&
        React.Component.prototype.isPrototypeOf(child.type.prototype) &&
        child.type.prototype instanceof MyComponent;

      if (isMyComponent) {
        return React.cloneElement(child, { isChild: true }); //newProps will get merged with the existing props
      } else {
        return child;
      }
    });
  }

  render() {
    return this.renderChildren();
  }
}

class Button extends MyComponent {
  render() {
    return (
      <View {...this.getProps()}>
        <ReactButton title={this.props.title} onPress={this.props.onPress} />
      </View>
    );
  }
}

I get the following error at the render method of the Button class:

Property 'render' in type 'Button' is not assignable to the same property in base type 'MyComponent'. Type '() => Element' is not assignable to type '() => ReactElement[]'. Type 'Element' is not assignable to type 'ReactElement[]'. Property 'includes' is missing in type 'Element'.

How can I fix this? Thanks

3
  • 2
    Which piece of code does cause this error? Please, provide stackoverflow.com/help/mcve . Can you replicate it in typescriptlang.org/play ? Commented Apr 27, 2018 at 18:52
  • @estus, sorry for the late reply. I have modified my question with the whole code. You can create a new .tsx file and paste it into VSCode. Thanks Commented Apr 28, 2018 at 4:48
  • I have also asked another related question. stackoverflow.com/questions/50075893 Commented Apr 28, 2018 at 11:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.