Let's say I have an array that might be empty:
const cheeses: string[] = ...
console.log('my favorite cheese is ',
cheeses.length > 0 ? cheeses.at(-1)?.toUpperCase() : 'GORGONZOLA'
)
This throws a type error at cheeses.at(-1): Object is possibly undefined.
I can fix it with (much uglier) cheeses[cheeses.length - 1].toUpperCase() (though that in fact seems to circumvent any type guard, since I can also call [cheeses.length - 10] without error.)
Is there a way to make Typescript (4.6.x) infer that cheeses.at(-1)? must be defined?