I'm working with TypeScript and React inside WebStorm. WebStorm is warning me "TS18048: b.firstName is possibly undefined" as well as for a.firstName
.
I have the below function which works, but as I'm new to TypeScript, I'm having a hard understanding how to get this possible error to go away in WebStorm without using ts-ignore
. I'm wondering if there is a better way to do this. I am having similar other things come up like this as well.
Can someone confirm if this is a proper way to build this function or enlighten me on a better approach?
export const sortByFirstName = (arr: Employee[]) => {
arr.sort((a, b) => {
if (a.firstName > b.firstName) return 1;
if (a.firstName < b.firstName) return -1;
return 0;
});
};