filter 메서드로 타입 거르기

filter 메서드를 통해서 null과 undefined를 걸렀음에도 결과 배열의 타입이 (string | null)[]로 나온다.

const array: (string | null)[] = ["foo", "bar", null, "zoo", null];
const filteredArray: string[] = array.filter(item => item) // ["foo", "bar", "zoo"]
// Type '(string | null)[]' is not assignable to type 'string[]' 

type predicates를 사용하도록 한다.

타입 단언과의 차이점은????

Documentation - Narrowing

TypeScript filter out nulls from an array