본문 바로가기
Q&A2026년 4월 18일1분 읽기

TypeScript에서 array.filter(Boolean) 써도 undefined가 타입에서 안 빠져요

프론트수진
조회 442댓글 4

배열에서 null/undefined 거르려고 filter(Boolean)을 쓰는데, 결과 타입이 그대로 (string | undefined)[]로 찍힙니다.

const xs: (string | undefined)[] = ["a", undefined, "b"];
const ys = xs.filter(Boolean);     // still (string | undefined)[]
ys.forEach(s => s.toUpperCase()); // ❌ 에러: s가 undefined일 수 있음

filter(Boolean)은 런타임에서는 falsy를 다 거르는데 TypeScript가 왜 이걸 좁혀주지 않는 건가요? tsconfig strict는 true입니다.

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록