私はTypeScriptとReactで作業しています。ダイアログウィンドウである私のコンポーネントでは、ボタンなどのトリガ要素をプロパティとして保存したいと考えています。コンポーネントがアンマウントすると、その要素にフォーカスを戻したいと思います。しかし、TSLintから、解決方法がわからないというエラーが表示されています。TypeScript - 'Element'はHTMLInputElementに割り当てられません
class Dialog extends React.Component<Props, {}> {
...
focusedElementBeforeDialogOpened: HTMLInputElement;
componentDidMount() {
this.focusedElementBeforeDialogOpened = document.activeElement;
...
}
...
}
私はプロパティの値を代入してい行にエラーを受信しています。私はElement
あるいはHTMLInputElement
にプロパティの種類を変更した場合しかし、私はエラーが表示さ
[ts] Type 'Element' is not assignable to type 'HTMLInputElement'.
Property 'accept' is missing in type 'Element'.
componentWillUnmount()
componentWillUnmount() {
...
this.focusedElementBeforeDialogOpened.focus();
}
このエラーは、focus()
方法を持たない要素タイプに関してです。
質問
はdocument.activeElement
は、入力タイプであることを活字体を伝える方法はありますか?
this.focusedElementBeforeDialogOpened = <HTMLInputElement>document.activeElement;
またはこの周りのより良い方法があるようなものはそうdocument.activeElement
と.focus()
の両方をサポートする型を宣言しますか? document.activeElementのドキュメントから
私が反応に慣れていないんだけど角度で、あなたは 'nativeElementを持っているが'プロパティ。あなたは 'activeElement'の後にそれを持っていませんか? – trichetriche