An expression of type 'void' cannot be tested for truthiness.ts(1345)(feat. firebase 회원가입 유효성검사)

bebrain·2023년 2월 9일
0

🔗Error Message

An expression of type 'void' cannot be tested for truthiness.ts(1345)

🔗 에러가 난 코드

    const validInputs = () => {
        if (!email) {
            alert("이메일을 입력하세요");
            // emailRef.current.focus();
        } else if (!email.match(emailRegex)) {
            alert("이메일형식에 맞게 입력해주세요");
        }

        if (!(pw || pwConfirm)) {
            alert("비밀번호 또는 비밀번호확인란이 비어있습니다");
        } else if (!pw.match(pwRegex)) {
            alert("비밀번호형식에 맞게 입력해주세요");
        } else if (pw !== pwConfirm) {
            alert("비밀번호 확인이 일치하지 않습니다");
        }

        if (!nickname) {
            alert("닉네임을 입력하세요");
        }
    };

문제점 : return값이 없다. 조건에 맞지 않으면 alert를 띄우고 그 즉시 validInputs함수가 종료되어야 하는데 저렇게 써 놓으면 alert만 띄우고 다음줄 그 다음줄도 열심히 읽게 된다. (if문은 return을 만나는 즉시 함수를 종료시킨다)

🔗 수정한 코드

    const validInputs = () => {
        if (!email) {
            alert("이메일을 입력하세요");
            emailRef.current!.focus();
            return true;
        } else if (!email.match(emailRegex)) {
            alert("이메일형식에 맞게 입력해주세요");
            emailRef.current!.focus();
            return true;
        }

        if (!(pw || pwConfirm)) {
            alert("비밀번호 또는 비밀번호확인란이 비어있습니다");
            pwRef.current!.focus();
            return true;
        } else if (!pw.match(pwRegex)) {
            alert("비밀번호형식에 맞게 입력해주세요");
            pwRef.current!.focus();
            return true;
        } else if (pw !== pwConfirm) {
            alert("비밀번호 확인이 일치하지 않습니다");
            pwConfirmRef.current!.focus();
            return true;
        }

        if (!nickname) {
            alert("닉네임을 입력하세요");
            nicknameRef.current!.focus();
            return true;
        }
    };

그러니까 여태 if문도 잘 모르고 썼다는 뜻

0개의 댓글