Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 'details': 'invalid authentication information.'}}
- codebuild no matching artifact paths found
- rds character_set 변경
- codepipeline 오류
- vscode시계아이콘
- route53 서브도메인
- 파이썬버전여러개
- fcm 멀티푸시
- 오블완
- 파이썬 경로 수정
- 맥북파이썬여러버전
- rds utf8mb4 변경
- ncp 401 authentication failed
- 티스토리챌린지
- command did not exit successfully docker-compose -f docker-compose.yml build exit status 1
- codebuild cliend_error
- 파이썬여러버전변경
- ncp authentication failed
- fcm multicast
- 'message': 'authentication failed'
- ncp api 응답오류
- fcm push
- vscode무한로딩
- 네이버클라우드 authentication failed
- rds 파라미터그룹
- vscode 소스제어
- {'error': {'errorcode': '200'
- 파이썬버전변경
- 파이썬 설치경로 변경
- vscode소스제어 무한로딩
Archives
- Today
- Total
All that I've dreamed of
React / TypeScript Property 'id' does not exist on type 'EventTarget' in TS 오류 본문
ETC
React / TypeScript Property 'id' does not exist on type 'EventTarget' in TS 오류
_베토디 2022. 12. 21. 11:39반응형
엘리먼트 요소에 변수로 id를 할당하고 엘리먼트를 클릭했을 때 id값을 함수 인자로 보내거나 state에 저장하고 싶었다.
<Box
id={date} // id 속성을 추가
onClick={(e) => {
getDailytask(e.target.id) // Box를 클릭하면 getDailytask의 인자로 보내주고 싶었다
}}>
위처럼 쓰면 TypeScript가 콘솔창 가득 에러메시지를 보낸다
Property 'id' does not exist on type 'EventTarget' in TS ...
EventTarget interface는 id라는 속성을 가지고 있지 않기 때문에
이벤트가 발생하는 엘리먼트의 타입선언(type assertion)을 해줘야 한다.
예) HTMLInputElement, HTMLButtonElement...
위 코드를 아래처럼 바꾸면 더 이상 오류 메시지 없이 엘리먼트의 속성 값을 사용할 수 있다.
<Box
id={date} // id 속성을 추가
onClick={(e) => {
const target = e.target as HTMLDivElement -> div요소를 타입선언
console.log(target.id)
getDailytask(target.id)
}}>
+
// inline으로 선언해줘도 된다
const result = (e.target as HTMLDivElement).value
console.log(result)
'ETC' 카테고리의 다른 글
[Github Actions] React + S3 + CloudFront 자동배포 (+ env 환경변수) (0) | 2023.01.08 |
---|---|
React 환경변수 개발/배포 env파일 분리 (+typescript) (0) | 2023.01.05 |
서브도메인 설정 (Route53, 가비아, cloudfront) (0) | 2023.01.05 |
AWS Cloud9에서 Lightsail (ec2) 인스턴스 열기 (0) | 2022.07.08 |
Github 계정 두개 사용하기 (0) | 2022.05.17 |
Comments