错误TS2339:类型“ HTMLProps <HTMLLabelElement>”上不存在属性“ for”


71

使用打字稿,并对带有明确键入类型定义的TSX文件做出反应,我得到了以下错误:

error TS2339: Property 'for' does not exist on type 'HTMLProps<HTMLLabelElement>'.

尝试使用以下TSX编译组件时

<label for={this.props.inputId} className="input-label">{this.props.label}</label>

我已经解决了,但是在这里添加了下一个人,因为该解决方案在搜索时未显示在任何地方(Google或StackOverflow)

Answers:


142

解决方案是将for属性更改为htmlFor

<label htmlFor={this.props.inputId} className="input-label">{this.props.label}</label>

这是React库本身的一部分,显然for像它一样处理不同class(它使用className),而不是肯定类型类型定义的问题。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.