投稿数 585
公開から 2940

Property 'value' does not exist on type EventTarget in TypeScript


Categories: typescript

いきさつ

エラーはまり。。

解決

Eventエレメントは基本的にHTMLElementが入ってるんだけど厳密には確約されているわけではいので 怒られたみたい。

なのでキャストを書いて明示的にHTMLElmenet入ってますよ〜ってTypeScriptさんに教えて上げる必要がある。

サンプル

こんなかんじ

button.addEventListener('click', event => {
        const element = event.target as HTMLElement;
        element.classList.add('-active');
    });

参考

https://stackoverflow.com/questions/44321326/property-value-does-not-exist-on-type-eventtarget-in-typescript