So I'm trying to nest a TypeScript React component within another, but it complains about types. It would seem it wants me to add all of my props into the parent interface?
Is there a way of doing this without having to have all my types listed in the child component interface, but then also having to add the types to the parent component interface?
Note: I am using Styled Components in the example below
interface IField {
children: React.ReactNode
}
export function Field({ children, htmlFor, label, required, ...props }: IField) {
return (
<FormField {...props}>
<Label htmlFor={htmlFor} label={label} required={required}/>
{children}
</FormField>
)
}
interface ILabel {
htmlFor: string
label: string
required?: boolean
}
export function Label({ htmlFor, label, required }: ILabel) {
return (
<FormLabel htmlFor={htmlFor}>
{label}
{required && (
<Required />
)}
</FormLabel>
)
}
Error I get:
Type '{}' is missing the following properties from type 'ILabel': htmlFor, label
Thanks for any help in advance!
Field's return? It looks like you're not passing in a required parameterhtmlForand you're also not passing in required correctly:props.requiredvsrequired-- as you're not destructuring that key directly.requiredprop. It's complaining on theLabelcomponent inside of theFormField.