sortArrayDate(arrayToSort, arrayDateKey, ascendingOrDecending) {
if (true) {
arrayToSort.sort(function(a, b){
if (a[arrayDateKey] === '' || a[arrayDateKey] === null) {
return 1;
}
if (b[arrayDateKey] === '' || b[arrayDateKey] === null) {
return -1;
}
return new Date(a[arrayDateKey]).getTime() - new Date(b[arrayDateKey]).getTime();
});
} else {
arrayToSort.sort(function(a, b){ //getting error
if (a[arrayDateKey] === '' || a[arrayDateKey] === null) {
return 1;
}
if (b[arrayDateKey] === '' || b[arrayDateKey] === null) {
return -1;
}
return new Date(b[arrayDateKey]).getTime() - new Date(a[arrayDateKey]).getTime();
});
}
}
I am getting the above error on the mentioned line.Whats the issue with the code. I am trying to sort dates from an array.
You have if condition on second line of function as:
if(true)
And then you have else part as well. Since first if will always be true, else can never be reached/called. That is why typescript is giving the unreachable code error.
If you want the code inside if to always execute, you can bring it out of if/else condition.
In order to disable this error without changing the code (not recommended), you can change the compiler config using tsconfig.json. Add this to disbale this warning:
"allowUnreachableCode": true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With