TypeScriptでreact-bootstrapのForm.ControlのonChangeイベントの型を付ける
Form.ControlのonChangeイベントだけじゃないかも。
React-bootstrap + TypeScriptにおいて、下記のようにForm.ControlのイベントハンドラhandleChange
の型をどうつければ良いのか分からなかった。
<Form.Control type="text" onChange={handleChange} />
vscodeで型推論されるのは((event: React.ChangeEvent<FormControlElement>) => void)
という型のようだが、その型を書いてもFormControlElement
というメンバはreact-bootstrap
には無いというエラーになる。
ググったら出てきた。
結局、(e: React.ChangeEvent<typeof FormControl & HTMLInputElement>) => void
という型をhandleChange
につけたらうまくいった。理由はよくわかっていない。