One of the challenges I encountered in my Angular project was with an interface defined in userinterface.ts
export interface Done {
wordlen: number;
word: string;
};
I utilized this interface to populate an array like so
donearr: Done[] = [];
let fill: Done = { wordlen: 4, word: "word" }
this.donearr.push(fill)
Everything seemed to be working fine up to this point. However, when I tried to process the last object in the array, it threw an error.
let v_last = this.donearr.pop()
console.log(v_last) // displays the content of the array
console.log(v_last.wordlen) // results in an error - 'v_last' is possibly 'undefined'.ts(18048)
// let v_last: Done | undefined
I attempted
let v_last: Done = this.donearr.pop()
but that also resulted in an error.
Type 'Done | undefined' is not assignable to type 'Done'.
Type 'undefined' is not assignable to type 'Done'.ts(2322)
Where am I going wrong?