reactjs - 'autoComplete' finns inte på typen 'IntrinsicAttributes & InputProps'
Jag använder Typescript med React och får detta fel:
Property 'autoComplete' does not exist on type 'IntrinsicAttributes & InputProps'.
Jag använder komponenten på detta sätt:
<Input
value={email}
id="email"
type="email"
label="email"
autoComplete="email"
placeholder="[email "
handleChange={event => setName(event.target.value)}
/>
Typescripten för props definieras så här:
type InputProps = {
label: string;
id: string;
type?: "text" | "email" | "date" | "number" | "tel";
helptext?: string;
handleChange: (event) => void;
value?: any;
validationMessage?: string[] | string;
validationType?:
| "help"
| "info"
| "invalid"
| "valid"
| "validating"
| "warning";
otherProps: any;
};
Eventuella överflödiga HTML-attribut vidarebefordras med otherProps.
function Input({
value = "",
type = "text",
handleChange,
label,
id,
helptext,
validationMessage,
validationType,
...otherProps
}: InputProps) {
Sedan läggs de andra egenskaperna på inputen:
<input value={value} onChange={handleChange} type={type} id={id} {...otherProps} />
Svar #1
Typen React.InputHTMLAttributes<HTMLInputElement> har all korrekt typinformation för de props som finns på <input>. Du skulle kunna utöka dina InputProps med denna typ och autoComplete-propen kommer då att vara tillgänglig med korrekt typinformation.