Type 'null' is not assignable to type 'HTMLInputElement' ReactJs

后端 未结 3 1538
粉色の甜心
粉色の甜心 2021-02-19 01:28

I am trying to reference data into reactJS along with typescript. While doing this I am getting below error

Type \'null\' is not assignable to type \'HTMLInputEl         


        
3条回答
  •  悲&欢浪女
    2021-02-19 01:30

    The error is produced becase the types definitions says input can be null or a HTMLInputElement

    You can set "strict": false in your tsconfig.json

    Or you can force the input to be HTMLInputElement type

     (this.textInput = thisInput as HTMLInputElement)} type="text" className="form-control" placeholder="Search" />
    

    This way is also valid (using definite assignment assertions (typescript >= 2.7))

     (this.textInput = thisInput!)} type="text" className="form-control" placeholder="Search" />
    

提交回复
热议问题