在我的反应和打字稿应用程序中,我使用:
onChange={(e) => data.motto = (e.target as any).value}
我如何正确定义类的类型,这样我就不必用 any
来破解类型系统了?
export interface InputProps extends React.HTMLProps<Input> {
...
}
export class Input extends React.Component<InputProps, {}> {
}
如果我把 target: { value: string };
我得到:
ERROR in [default] /react-onsenui.d.ts:87:18
Interface 'InputProps' incorrectly extends interface 'HTMLProps<Input>'.
Types of property 'target' are incompatible.
Type '{ value: string; }' is not assignable to type 'string'.
原文由 wildeyes 发布,翻译遵循 CC BY-SA 4.0 许可协议
通常事件处理程序应该使用
e.currentTarget.value
,例如:你可以在这里阅读为什么会这样( Revert “Make SyntheticEvent.target generic, not SyntheticEvent.currentTarget.” )。
UPD:正如@roger-gusmao
ChangeEvent
所述,更适合输入表单事件。