Closed
Description
I'm doing a extreme check to not get object undefined but typescript throw the error Object is possibly 'undefined'
anyway
TypeScript Version: 2.7.2
Search Terms:
Code
if (state && state.people && state.people.length) {
state.people = state.people.sort((person1: IPerson, person2: IPerson) => {
const orderBy = state.orderBy;
if ( orderBy
&& person1
&& person2
&& Object.keys(person1).length
&& Object.keys(person2).length
&& person1[orderBy]
&& person2[orderBy]) {
if (person1[orderBy] < person2[orderBy]) {
return (state.sort) ? -1 : 1;
}
if (person1[orderBy] > person2[orderBy]) {
return (state.sort) ? 1 : -1;
}
}
return 0;
});
}
Expected behavior:
it shouldn't throw the error Object is possibly 'undefined'
Actual behavior:
It throws the error Object is possibly 'undefined'
in these if
if (person1[orderBy] > person2[orderBy])
if (person1[orderBy] < person2[orderBy])
Playground Link:
Related Issues: