【发布时间】:2020-01-13 11:09:12
【问题描述】:
我正在使用带有 React 的 Typescript 并收到此错误:
Property 'autoComplete' does not exist on type 'IntrinsicAttributes & InputProps'.
我正在使用这样的组件:
<Input
value={email}
id="email"
type="email"
label="email"
autoComplete="email"
placeholder="Joe@hotmail.com"
handleChange={event => setName(event.target.value)}
/>
道具的打字稿是这样定义的:
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;
};
任何多余的 HTML 属性都通过 otherProps 传递。
function Input({
value = "",
type = "text",
handleChange,
label,
id,
helptext,
validationMessage,
validationType,
...otherProps
}: InputProps) {
然后将 otherProps 放在输入上:
<input value={value} onChange={handleChange} type={type} id={id} {...otherProps} />
【问题讨论】:
-
Input组件来自哪里?而且,你可以控制type InputProps吗?
标签: reactjs typescript