(Typescipt) The left-hand side of assignment expression... Error

Mirrer·2023년 3월 24일
0

Error Handling

목록 보기
5/7
post-thumbnail

Problem Definition

Typescipt의 빌드 과정에서 발생하는 문제 해결 과정

Typescript를 사용하면서 다음과 같은 "The left-hand side of assignment expression may not be an optional property access" 에러가 발생했다.


Resolution

해당 오류를 해석한 결과 '할당식의 왼쪽에는 Optional 연산자를 사용하면 안된다'라는 의미였다.

그래서 해당 변수가 undefined이 아니라는 확실한 조건을 추가, 즉 변수가 undefined가 아닐 때만 해당 로직을 실행하여 문제를 해결했다.


  • 기존 코드
state.singlePost?.comments = action.payload;
  • 수정 코드
if (state.singlePost?.comments !== undefined) {
  state.singlePost.comments = action.payload;
}
profile
memories Of A front-end web developer

0개의 댓글