简体   繁体   English

TS2339:类型“ {}”上不存在属性“焦点”。 使用React打字稿

[英]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.

相关问题 Typescript TS2339:在Typescript + React + Redux应用程序中,类型&#39;IntrinsicAttributes&#39;上不存在属性&#39;queryType&#39;吗? - Typescript TS2339: Property 'queryType' does not exist on type 'IntrinsicAttributes' in Typescript + React + Redux app? 反应,Typescript,钩子 - 从钩子中解构数据,TS2339:类型上不存在属性“数据” - React, Typescript, Hooks - destructuring data from hook, TS2339: Property 'data' does not exist on type TS2339:移植到Typescript时,类型'typeof React'上不存在属性'PropTypes' - TS2339: Property 'PropTypes' does not exist on type 'typeof React' when porting to Typescript 类型“X[]”上不存在属性“customProperty”。 打字稿反应。 TS2339 - Property 'customProperty' does not exist on type 'X[]'. Typescript React. TS2339 React typescript,类型“typeof TodoFilterItem”上不存在属性“propTypes”。 TS2339 - React typescript, Property 'propTypes' does not exist on type 'typeof TodoFilterItem'. TS2339 children.toArray() - TS2339:“ReactChild | 类型”上不存在属性“键” 反应片段 | ReactPortal'... - 反应 Typescript - children.toArray() - TS2339: Property 'key' does not exist on type 'ReactChild | ReactFragment | ReactPortal'... - React Typescript 类型“HTMLInputElement”上不存在属性“目标”。 TypeScript 反应。 TS2339 - Property 'target' does not exist on type 'HTMLInputElement'. TypeScript React. TS2339 TS2339(TS)属性“状态”在类型上不存在 - TS2339 (TS) Property 'state' does not exist on type 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339 Typescript 2.6-&gt;使用模块扩展来扩展外部库的类型定义-&gt; TS2339:类型”上不存在属性” - Typescript 2.6 -> Extend type definition for external library with module augmentation -> TS2339: Property '' does not exist on type ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM