codestates / codestates/rightnow
[Error-Handling] element에서 설정한 property를 e.target으로 가져올 수 없음
Open
err-handling
- Dominant language
- TypeScript
- Stars
- 0
- Forks
- 3
- PR merge metrics
- No merged PRs in 30d
Description
### **어떤 에러인가요?**
- element에 property를 설정하고, 이벤트 리스너를 사용하여 e.target에 담긴 propery 값을 사용하려고 했는데, 가져올 수 없고, EventTarget에 id property가 없다는 에러가 뜸
### **에러 메시지**
`TS2339: Property 'id' does not exist on type 'EventTarget'.`
### **에러 핸들링 방법**
- 구글링을 해본 결과, 타입스크립트가 target의 타입을 특정할 수 없기 때문에 발생하는 오류라는 것을 알았다.
- 때문에 target의 타입을 특정하기 위해 `as`를 사용하여 내가 사용하려는 타입인 `HTMLDivElement`로 형변환을 해주어 에러를 없앨 수 있었다.
```js
const { id } = e.target as HTMLDivElement;
```
### **에러 핸들링을 위해 참고한 레퍼런스 링크**
[링크](https://freshman.tech/snippets/typescript/fix-value-not-exist-eventtarget/)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.