Closed as not planned
Closed as not planned
Description
π Search Terms
Control flow analysis async race condition
π Version & Regression Information
- This changed between versions 5.4.5 and 5.5.0
- This changed in Control flow analysis for element access with variable indexΒ #57847
β― Playground Link
π» Code
async function getName(): Promise<string> {
return 'world';
}
async function f1(obj: Record<string, ((x: string) => void) | undefined>, key: string) {
if (typeof obj[key] === "function") {
const name = await getName();
obj[key](name);
}
obj[key] = undefined;
}
const key = 'greeting';
const obj = {
[key](name: string): void {
console.log(`Hello, ${name}!`);
}
}
Promise.all([f1(obj, key), f1(obj, key)]);
π Actual behavior
No type error, but runtime failure:
Unhandled Promise Rejection: TypeError: obj[key] is not a function. (In 'obj[key](name)', 'obj[key]' is undefined)
π Expected behavior
Type checker should account for potential race condition and do not persist the narrowed type after an await
statement.
Additional information about the issue
No response