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)]);
Unhandled Promise Rejection: TypeError: obj[key] is not a function. (In 'obj[key](name)', 'obj[key]' is undefined)
Type checker should account for potential race condition and do not persist the narrowed type after an await statement.
π Search Terms
Control flow analysis async race condition
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.1-rc#code/IYZwngdgxgBAZgV2gFwJYHsIwOYFNkBywAtrgBQCUAXDAAoBO6xqIuAPCMvahNgHwwA3gCgYYmPXwJ6WAOQB3dPQA2AE1kBuYQF9hw0JFiIUGLHACMZdACMAVjQBKuKEtUcuPbABoYZMgA8aTm5eChgAXgEAN3RUVTCAHxgkVVw4HlxVPh8Aa1wwII9QoVFxVDhfZDAAB1x0CptbAG08sABdCPDwmAAiYyg0TB6wkXExmBcIThgIElwImGB5YFRkHHwiUkotcfFGlvy2slnSCh3xXTH91o7ulLSM1S1dYUnp1oXZbEl8T01XzDTRoLUbiA7tY5zQohbDUGAxOIlXZiN7oZS4AB0ynQ2DIAAMABK4ZTYnwAEkEJ1w2gAhHizqUxLoXgwmCxMcASWQmhYrHZcvkKD5eY0BWAKG0GUA
π» Code
π Actual behavior
No type error, but runtime failure:
π Expected behavior
Type checker should account for potential race condition and do not persist the narrowed type after an
awaitstatement.Additional information about the issue
No response