Question #239

Author: admin
tags: TypeScript  
interface Car {
  manufacturer: string,
}

interface City {
  population: number,
}

const arr: (City | Car)[] = [{ population: 100 }, { manufacturer: 'ABC' }];

const filteredArr = arr.filter((item) => 'manufacturer' in item); // ??
What is type of filteredArr?
(City | Car)[]
City[]
Car[]
In this situation we should help Typescript to narrow type using type predicate:
const filteredArr = arr.filter((item): item is Car => 'manufacturer' in item); // filteredArr now has Car[] type
Rate the difficulty of the question:
easyhard