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

How to describe type of props for children #102

Closed
deser opened this issue Apr 5, 2019 · 14 comments
Closed

How to describe type of props for children #102

deser opened this issue Apr 5, 2019 · 14 comments
Labels
wontfix This will not be worked on

Comments

@deser
Copy link

deser commented Apr 5, 2019

// item is of PageItem type

const controls = _.map(items, item => <FVDesignerItem key={item.id} item={item} />);
        return (
            <FvPageWrapper>
                <FvPositioningContainer disabled={disabled}>{controls}</FvPositioningContainer>
            </FvPageWrapper>
        );

...
// FvPositioningContainer .tsx

React.Children.map(children, (child, i) => {
            if (!child) return undefined;
            const { x, y, w, h } = child.props.item;  // item here is of PageItem , propagated automatically and validated by tsc
           ....
        });
@swyxio
Copy link
Collaborator

swyxio commented Apr 8, 2019

this is potentially very useful for us! cc @ferdaber i wonder what problems you and Ryan Florence ran into that prevented him typing children - even if there are problems, we should definitely document them

@ferdaber
Copy link
Collaborator

ferdaber commented Apr 9, 2019

Typing children as anything but JSX is generally safe for TypeScript. It only becomes a problem if you try typing them as specific JSX types.

So if you say "I want my children to be an array of objects with these properties" 👍🏼, but "I want my children to be an array of JSX elements that have this component as their type/constructor" 👎🏼

@ferdaber
Copy link
Collaborator

ferdaber commented Apr 9, 2019

In the case above they are just simple interfaces, or objects, so they are able to be well-typed as children.

@swyxio
Copy link
Collaborator

swyxio commented Apr 9, 2019

ah ok. honestly wouldnt have been a blocker for me adopting ts in Reach UI

@deser
Copy link
Author

deser commented Apr 9, 2019

In the case above they are just simple interfaces, or objects, so they are able to be well-typed as children.

Hm, seems I either misunderstood smth or that is not entirely correct. By default (without any explicit typings) typescript blames that child (which is React.Node) can't have any props:
image

Whatever I do with explicit typing doesn't help eventually.

@deser
Copy link
Author

deser commented Apr 9, 2019

Maybe this is an issue with @types/react or that I have React 15.6 installed but not 16.*

@ferdaber
Copy link
Collaborator

ferdaber commented Apr 9, 2019

It's honestly a bit difficult for me to decipher what you want to achieve with the short example at the beginning of the issue. Can you elaborate on it?

@eps1lon
Copy link
Member

eps1lon commented Apr 9, 2019

Shouldn't this cover most use cases:

interface ParentProps {
  children: React.ReactElement<ChildProps>;
}

interface ChildProps {
  value: string;
}

function Parent(props: ParentProps) {
  return <div>My child has {props.children.props.value} value.</div>
}

// { children: string } is not assignable to ...
<Parent>1</Parent>; // $ExpectError
<Parent><input value="0" /></Parent>;

You could also allow only numbers or arrays. As long as it's assignable to React.ReactNode you shouldn't have any troubles. Probably helps with safe cloneElement but I would personally not use this anymore. This pattern creates a tight parent-child coupling that doesn't allow intermediate components (goes against reacts composition model). Context should cover most use cases.

@ferdaber
Copy link
Collaborator

ferdaber commented Apr 9, 2019

@eps1lon this actually doesn't work because when you construct <input value="0" /> the type of the resulting expression is just JSX.Element<any> so TypeScript won't actually check the constructor type of the child JSX expression, you can basically put any JSX expression you want inside.

@eps1lon
Copy link
Member

eps1lon commented Apr 9, 2019

@eps1lon [...] you can basically put any JSX expression you want inside.

😞 Well at least you could restrict it to primitives? Still has some DX improvements in the parent. For more usage safety you could add a prop-types validator e.g.

Parent.propTypes = {
  children: PropTypes.shape({
    props: PropTypes.shape({ // could share `propTypes` to the child
      value: PropTypes.string.isRequired
    })
  }).isRequired
};

@ferdaber
Copy link
Collaborator

ferdaber commented Apr 9, 2019

Yep, that's kind of what I was alluding to in my first response. TypeScript will have no problems with simple interfaces, so the implementing component will just need to wrap it in a createElement call. It's an unfortunate limitation :/

@deser
Copy link
Author

deser commented Apr 9, 2019

Yep, that's kind of what I was alluding to in my first response. TypeScript will have no problems with simple interfaces, so the implementing component will just need to wrap it in a createElement call. It's an unfortunate limitation :/

@ferdaber , sorry I don't quite understand what does it mean to wrap it in a createElement call. Could you be so kind to show an example?

Thanks

@stale
Copy link

stale bot commented Aug 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions!

@stale stale bot added the wontfix This will not be worked on label Aug 20, 2020
@swyxio
Copy link
Collaborator

swyxio commented Aug 21, 2020

cleaning up old issues - pls reopen if you still face this!

@swyxio swyxio closed this as completed Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants