I have an interface Employee:
export interface Employee {
firstName: string;
lastName: string;
}
I am trying to push elements of Type Employee into an array as below:
let allEmployees: Employee[] = [];
allEmployees = emp.data?.map((employee) => {
return { firstName: employee.firstName, lastName: employee.lastName }
});
I am getting the following typescript compile time error:
type 'undefined' is not assignable to type 'Employee[]'
I have used .map function to return final value of the array. How do I resolve this issue ?
Thanks
empis important here. I presume itsdataproperty might itself beundefined, and therefore the optional chaining operatoremp.data?.map()will possibly setallEmployeestoundefinedwithout ever runningmap(). Also, I don't see you trying to push any elements; instead you're trying to reassignallEmployees. Do you want to try topush()instead?