[TS] Argument of type '' is not assignable to parameter of type 'never'.

지원·2024년 5월 11일

!error

목록 보기
5/9

Argument of type '{ id: number; nick: string; type: string; date: string; }' is not assignable to parameter of type 'never'.

reduce()로 데이터 필터링 하다가 발생한 에러

문제가 된 코드

const { noticeToday, noticePast } = noticedata.reduce(
    (acc, notice) => {
      if (notice.date === arrangeToday) {
        acc.noticeToday.push(notice);
      } else {
        acc.noticePast.push(notice);
      }
      return acc;
    },
    { noticeToday: [], noticePast: [] }
  );

해결

배열 타입을 지정해주지 않아서 디폴트로 never타입이 돼서 발생함.

never는 없는 값의 집합이다.
집합의 값이 없기 때문에 어떠한 값(any 포함)도 가지지 못한다.

그래서 push했을 때 에러가 난 것임

const { noticeToday, noticePast } = noticedata.reduce(
    (acc, notice) => {
      if (notice.date === arrangeToday) {
        acc.noticeToday.push(notice);
      } else {
        acc.noticePast.push(notice);
      }
      return acc;
    },
    { noticeToday: [] as any, noticePast: [] as any }
		// { noticeToday: [{}], noticePast: [{}] }
		// ㄴ 이렇게 해도 오류없이 push는 되지만 0번째에 빈 데이터가 들어가게 됨
  );

이거 보고 해결함
https://stackoverflow.com/questions/52423842/what-is-not-assignable-to-parameter-of-type-never-error-in-typescript
never란-무엇인가

타입스크립트 완전 알못 작년에는 저렇게 해결했고, 지금 다시 생각해보면
type을 정의하고

const [noticeToday, setNoticeToday] = useState<Type[]>();

이런 식으로 미리 선언하는 방식으로 해볼듯
as any...? 벌써 찝찝함
실제로 실행은 안해봐서 오류가 안날 지는 모르겠당
안나지 않을까?

0개의 댓글