So this will output an <input> element - everything works perfectly:
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
error?: FieldError;
icon?: string;
id: string;
register?: UseFormRegisterReturn;
}
const StyledInputComponent = styled.input`
...
`
const MyComponent = ({
error,
icon,
id,
register,
onChange,
...props
}: InputProps) => {
return (
<StyledInputComponent
hasError={!!error}
id={id}
onChange={onChange}
placeholder={placeholder}
type={type}
{...register}
{...props}
/>
});
};
I now need to be able to have the consuming component choose between an <input> and a <textarea>.
The problem I have is that (I think) I need to extend the interface further like this:
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement>, React.InputHTMLAttributes<HTMLTextAreaElement> {
error?: FieldError;
icon?: string;
id: string;
inputType?: "input" | "textarea";
register?: UseFormRegisterReturn;
}
And I'm just using the new inputType prop to switch between outputted components:
{ inputType === "input" &&
<StyledInputComponent
hasError={!!error}
id={id}
onChange={onChange}
placeholder={placeholder}
type={type}
{...register}
{...props}
/>
}
{ inputType === "textarea" &&
<StyledTextAreaComponent
hasError={!!error}
id={id}
onChange={onChange}
placeholder={placeholder}
rows={4}
{...register}
{...props}
/>
}
However, trying to extend the interface for both an <input> and a <textarea> has lead to numerous errors, all along these lines:

What's the right way to go about resolving this?