getPersonneById(id: number): Personne{
const personne = this.personnes.find( personne => {
return personne.id == id;
});
return personne;
}
An error message is displayed: Unable to assign type 'Person | undefined' to type 'Person'. Unable to assign type 'undefined' to type 'Person'.
I have also tried the following workaround, although it is not ideal:
getPersonneById(id: number): Personne{
const personne = this.personnes.find( personne => {
return personne.id == id;
});
if(typeof personne !== 'undefined'){
return personne;
}else{
return this.personnes['0'];
}
}
The variable 'personnes' is an array.
My question is whether there is a way to ensure that the variable cannot be undefined or if there is a better solution.