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
Comments
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 |
Typing children as anything but 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" 👎🏼 |
In the case above they are just simple interfaces, or objects, so they are able to be well-typed as children. |
ah ok. honestly wouldnt have been a blocker for me adopting ts in Reach UI |
Maybe this is an issue with @types/react or that I have React 15.6 installed but not 16.* |
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? |
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 |
@eps1lon this actually doesn't work because when you construct |
😞 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
}; |
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 |
@ferdaber , sorry I don't quite understand what does it mean to Thanks |
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! |
cleaning up old issues - pls reopen if you still face this! |
The text was updated successfully, but these errors were encountered: