Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you provide an example for type checking JSX childrens? #7

Open
veeramarni opened this issue Aug 9, 2017 · 3 comments
Open

Can you provide an example for type checking JSX childrens? #7

veeramarni opened this issue Aug 9, 2017 · 3 comments
Assignees
Milestone

Comments

@veeramarni
Copy link

microsoft/TypeScript#13618

Example: We create High-level component package with strong typed how the lower components designed to be.

type Avatar: () => JSX.Element;
interface ILoginProps {
    accounts: object;
    setFormType: Function;
    children: Avatar;
}
class Login extends React.Component<ILoginProps, ILoginState> {
    public static propTypes = {
        Avatar: React.PropTypes.func,
 }
public render(): JSX.Element {

 const { Avatar } = this.props
 return (<Avatar/>)
}

@veeramarni veeramarni changed the title Can you given an example with model? Can you provide an example with model? Aug 10, 2017
@piotrwitek piotrwitek self-assigned this Aug 28, 2017
@piotrwitek piotrwitek added this to the Next milestone Dec 23, 2017
@IssueHuntBot
Copy link

@BoostIO funded this issue with $25. Visit this issue on Issuehunt

@IssueHuntBot
Copy link

@issuehuntfest has funded $5.00 to this issue. See it on IssueHunt

@piotrwitek piotrwitek changed the title Can you provide an example with model? Can you provide an example for type checking JSX childrens? Feb 12, 2019
@mjsarfatti
Copy link

You have to assert the child type within the component, rather than relying on the props.
Something like this should work:

// your lower component
type AllowedChildComponentProps = { ... }
const AllowedChildComponent = [...];

// the high level component
type YourComponentProps = {
  children?: React.ReactElement<AllowedChildComponentProps> | React.ReactElement<AllowedChildComponentProps>[]
}

const YourComponent = (props: YourComponentProps) => {
  return (
    {children && React.Children.map(children, child => {
      if ((child as ReactElement).type === AllowedChildComponent) {
        // go ahead
      } else {
        // throw or something
      }
    }
  );
}

More info: https://stackoverflow.com/q/44475309/416714

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants