Refine object unions by checking for a property that only one variant has. Use 'pages' in x vs 'duration' in x to decide how to read the value. Reduce the array to a single number for easy comparison.
function metric(x: { pages: number } | { duration: number }) {
return "pages" in x ? x.pages : x.duration;
}Sum the metric across the whole list.
Sum pages for books and minutes for movies.
type Book = { title: string; pages: number } and type Movie = { title: string; duration: number }.type Media = Book | Movie.totalWork(items: Media[]): number that adds up pages for books and duration for movies.