针对React JS FormControl事件的Typesafe onChange

时间:2017-11-30 21:06:22

标签: reactjs typescript types

我有一个处理更改文本字段的函数:

private handleNameChange(e: React.FormEvent<FormControl>) {
    const name = e.target.value;
    this.setState({ name });
    this.props.editGroupName(name);
}

这是连接到FormControl的onChange:

<FormControl type='text' placeholder='Name' value={this.state.name} onChange={this.handleNameChange} />

然而,根据TypeScript,这是错误的:

TS2339: Property 'value' does not exist on type 'EventTarget'.

我也尝试过使用currentTarget。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

尝试将e: React.FormEvent<FormControl>替换为e: React.FormEvent<FormControlProps>