타입스크립트에서의 reduce 사용방법

GJ·2023년 6월 2일
0

문제인식

타입스트립트에서 타이핑이 적용된 배열에 대해 reduce를 사용하려고 하면 타입 에러가 발생한다.

해결방법

초기 배열이 일반 배열로 선언되기 때문에 발생하는 에러이다.
초기 배열을 배열의 타이핑으로 인식시켜주면 에러가 더 이상 발생하지 않는다.

['a', 'b', 'c'].reduce((accumulator, value) => accumulator.concat(value), [] as string[]);

참조

https://stackoverflow.com/questions/54117100/why-does-typescript-infer-the-never-type-when-reducing-an-array-with-concat

profile
Frontend Developer

0개의 댓글