Specifically,
function foo(a: number[]) : number {
let result = false;
a.forEach((x) => {
if (x === 5) {
result = true;
}
});
// TypeScript warns that 'result' always returns false
if (result === true) {
return 1;
}
return 0;
}
In this scenario, TypeScript does not recognize that 'result' can actually be true due to the assignment inside the callback function. You can experiment with it in this playground link
You could potentially restructure the code using 'Array#some', but in certain cases like when using 'map' to generate a result, it might be more efficient to avoid an additional iteration over the array just to set a flag.
Is there any way to improve the recognition of whether the callback function has been executed?