[英]TS2339: Property 'focus' does not exist on type '{}'. with React typescript
I have a typescript code for JSX class 我有JSX类的打字稿代码
export class SearchForm extends React.PureComponent<Props, State> {
public inputRef: React.RefObject<{}>;
constructor(props: any) {
super(props)
this.inputRef = React.createRef()
}
public componentDidMount(): void {
this.inputRef.current.focus()
this.inputRef.current.select()
...
Now when I try to compile this code, I have a bunch of errors: 现在,当我尝试编译此代码时,我遇到了很多错误:
ERROR in ...
TS2339: Property 'className' does not exist on type '{}'.
ERROR in ...
TS2339: Property 'focus' does not exist on type '{}'.
What is an issue? 有什么问题?
The error is in type definition of the inputRef: React.RefObject<{}>;
错误在于
inputRef: React.RefObject<{}>;
类型定义inputRef: React.RefObject<{}>;
, which is default suggestion for autofixing the type issue. ,这是自动修复类型问题的默认建议。 Type
RefObject<{}>
is not assignable to type RefObject<HTMLInputElement>
. 不能将类型
RefObject<{}>
分配给类型RefObject<HTMLInputElement>
。 Type {}
is missing the following properties from type HTMLInputElement
: accept, align, alt, autocomplete, and more. 类型
{}
缺少HTMLInputElement
类型的以下属性:接受,对齐,替代,自动完成等。
The correct row for public inputRef: React.RefObject<{}>;
public inputRef: React.RefObject<{}>;
的正确行public inputRef: React.RefObject<{}>;
should be: 应该:
public inputRef: React.RefObject<HTMLInputElement>;
And the the piece of the code will look like: 这段代码看起来像:
export class SearchForm extends React.PureComponent<Props, State> {
public inputRef: React.RefObject<HTMLInputElement>;
constructor(props: any) {
super(props)
this.inputRef = React.createRef()
}
public componentDidMount(): void {
this.inputRef.current.focus()
this.inputRef.current.select()
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.