[Form] form onSubmit에서 event객체에 value 프로퍼티 없을경우 해결

Darcy Daeseok YU ·2023년 8월 6일
0

onSubmit에서 input 값을 보고 싶은데... 타입스크립트에서 오류가 난다.

  onSubmit={(e) => {
          e.preventDefault();

          const topic = e.currentTarget.topic;

          const content = e.currentTarget.content;

          function assertIsFormFieldElement(
              element: Element | HTMLSelectElement | HTMLButtonElement,
            ): asserts element is HTMLInputElement {
              if (!('value' in element)) throw new Error('폼 요소가 아님');
            }

          assertIsFormFieldElement(topic);

          console.log("values ==== ", topic.value, content);
        }}

해결안은 assert 함수를 만들어서 해당 오브젝트에 value 프로퍼티가 있는지 체크한다.

profile
React, React-Native https://darcyu83.netlify.app/

1개의 댓글

comment-user-thumbnail
2023년 8월 6일

이런 유용한 정보를 나눠주셔서 감사합니다.

답글 달기