我有一个如下所示的输入:
<input
type="text"
value={form().username}
onInput={(e) => handleInput(e)}
/>
而TypeScrip表明,e
是这样的类型:
(parameter) e: InputEvent & {
currentTarget: HTMLInputElement;
target: Element;
}
如果我使用它创建handleInput,则使用名称和值不会显示为类型定义的一部分,所以我添加了它们:
const handleInput = (e: InputEvent & {
currentTarget: HTMLInputElement;
target: Element & { name: string; value: string; };
}) => {
setForm({
...form(),
[e.target.name]: e.target.value,
});
}
但是,这会使我的e
在输入文本框中突出显示为错误:
Argument of type 'InputEvent & { currentTarget: HTMLInputElement; target: Element; }' is not assignable to parameter of type 'InputEvent & { currentTarget: HTMLInputElement; target: Element & { name: string; value: string; }; }'.
Type 'InputEvent & { currentTarget: HTMLInputElement; target: Element; }' is not assignable to type '{ currentTarget: HTMLInputElement; target: Element & { name: string; value: string; }; }'.
Types of property 'target' are incompatible.
Type 'EventTarget & Element' is not assignable to type 'Element & { name: string; value: string; }'.
Type 'EventTarget & Element' is missing the following properties from type '{ name: string; value: string; }': name, value
如何修复这种不兼容性,因为名称和值是事件目标类型的有效部分.