라벨이 useRef인 게시물 표시

ts(2322), ts(2339) : typescript에서 useRef 사용할때 발생하는 에러 처리

이미지
ts(2322) 에러 Type 'undefined' is not assignable to type 'HTMLInputElement | null'. ts(2322) // 1번 const inputRef = useRef () ; // 2번 const inputRef = useRef ( null ) ; useRef 선언시 null 값을 작성하지 않아서 발생하는 에러이다. 1번을 2번처럼 변경하여 오류를 해결할 수 있다. ts(2339) 에러 Property 'focus' does not exist on type 'never'. ts(2339) const inputRef = useRef < HTMLInputElement > ( null ) ; useRef<해당하는 엘리먼트 객체> 를 작성해주면 해당 오류를 해결할 수 있다.