I'm running into a @typescript-link no-unnecessary-condition warning with the following code (source):
async function waitForOneOf(locators: Locator[]): Promise<WaitForRes> {
// timeToWait can be long because if it fails, the whole test fails anyway.
const timeToWait = 5000;
const res = await Promise.race([
...locators.map(async (locator, index): Promise<WaitForRes> => {
let timedOut = false;
await locator
.waitFor({ state: 'visible', timeout: timeToWait })
.catch(() => {
timedOut = true;
});
return [timedOut ? -1 : index, locator];
}),
]);
if (res[0] === -1) {
throw new Error(`no locator visible before timeout ${locators.toString()}`);
}
return res;
}
The problem is the line
return [timedOut ? -1 : index, locator];
Eslint identifies timedOut
is always falsy because let timedOut = false
. However, I think this is incorrect (it is not always falsy) becaue the value of timedOut
may be changed in the catch function because it is an arrow function, so the timedOut
variable is accessible to it.
Is my understand correct-- this eslint error is incorrect? Or am I misunderstanding how variables work in JS/TS in this scenario?