I have the below array which can have either be an object or undefined. Even after filtering the results to contain all defined values, typescript still says it could be undefined
interface Bob {
name: string;
}
type BobDetails = undefined | Bob;
const myArray: BobDetails[] = [{ name: "Bob"}, undefined];
myArray[0].name; // expected Object is possibly 'undefined'.(2532)
var filter = myArray.filter(arr => arr?.name);
filter[0].name; // still Object is possibly 'undefined'.(2532)
How do make Typescript know that filter[]
will have only Bob[] and no undefined. I am aware we might be able to do with !
but I want the normal flow to realise it.