TS2339: Property 'focus' does not exist on type '{}'. with React typescript
The error is in type definition of the inputRef: React.RefObject<{}>;
, which is default suggestion for autofixing the type issue.Type RefObject<{}>
is not assignable to type RefObject<HTMLInputElement>
.Type {}
is missing the following properties from type HTMLInputElement
: accept, align, alt, autocomplete, and more.
The correct row for 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() ...