I have a component that accepts a generic type parameter TChildProps:
type GenericFieldHTMLAttributes =
| InputHTMLAttributes<HTMLInputElement>
| SelectHTMLAttributes<HTMLSelectElement>
| TextareaHTMLAttributes<HTMLTextAreaElement>
interface FieldProps {
name: string
label?: string | React.ReactNode
component?: string | React.ComponentType
initialValue?: any
children?: React.ReactNode | FieldRenderFunction
}
class Field<TChildProps = GenericFieldHTMLAttributes> extends React.PureComponent<FieldProps & TChildProps> {
...
}
When I use this component, I would expect it to prevent me from passing in unrecognized props, for example:
render() {
return (
<Form onSubmit={this.save}>
<Field foo="test" label="email" name="email" type="email" component={TextField} />
</Form>
)
}
Surprisingly, the above code compiles without even any warnings, despite the fact that the foo prop is not defined anywhere. I tried simplifying the example and got the same result:
class Field<TChildProps = {}> extends React.PureComponent<FieldProps & TChildProps> {
...
}
// this still compiles without errors
<Field foo="test" label="email" name="email" type="email" component={TextField} />
Is TypeScript functioning the way it's supposed to here according to the type definition for React.PureComponent (by the way I tested it on React.Component and got the same result), or is this a bug?