[typescript error] TS2345: 'HTMLElement | null'

KoEunseo·2023년 1월 11일
0

typescript

목록 보기
4/23

TS2345: Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element | DocumentFragment'.

위와 같은 에러가 떴다.
document.getElementById('root') 부분이다.
찾아보니 타입이 정해진 것을 다른 자료형에 할당하려고 하면 발생하는 에러라고 한다.

document.getElementById('root')!
이렇게 ! 하나 붙이면 해결.
null이 아니어야 한다는 의미이다.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html

profile
주니어 플러터 개발자의 고군분투기

1개의 댓글

comment-user-thumbnail
2024년 1월 27일

공부하다가 우연히 봤는데 다른글을 비교하니
document.getElementById("root") as HTMLElement 와 같이 타입을 정해주는게 정석인것 같아요.

답글 달기