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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39187722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:48:04  来源:igfitidea点击:

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

reactjstypescript

提问by Matthew Beatty

Using typescript and react with TSX files with definitely typed type definitions, I am getting the error:

使用打字稿并对具有明确类型定义的 TSX 文件做出反应,我收到错误消息:

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

When trying to compile a component with the following TSX

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

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

I have already solved but adding here for the next person since the solution didn't show up anywhere when searching (Google or StackOverflow)

我已经解决了,但在这里添加给下一个人,因为搜索时解决方案没有出现在任何地方(Google 或 StackOverflow)

回答by Matthew Beatty

The solution was to change the forattribute to htmlFor

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

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

This is a part of the React library itself which apparently handles fordifferently just like it does class(it uses className) and not an issue with the definitely typed type definitions.

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