Skip to content

TypeScript null 체크

jin-Pro edited this page Nov 15, 2021 · 2 revisions

문제점

if( ref.current === null ) return;

const data = ref.current.value;

위 코드를

function checkNull(target) {
    if(target === null) return true;
    return false;
}

if ( checkNull(ref.current) ) return;

const data = ref.current.value;

이렇게 바꾸었으나, const data = ref.current.value 부분에서 개체가 Null인것 같다는 에러가 발생.

결국 아래와 같이 수정을 하였는데 괜찮은 코드인지 몰라서 질문드리게 되었습니다.

if( !ref.current ) return;

const data = ref.current.value;
Clone this wiki locally