Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect narrowing of boolean type when variable is mutated in a timer #61445

Closed
TAGraves opened this issue Mar 18, 2025 · 2 comments
Closed

Comments

@TAGraves
Copy link

πŸ”Ž Search Terms

"no-unnecessary-condition", "boolean", "constant", "mutated", "conditional", "timer"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about setTimeout

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250318&ssl=9&ssc=1&pln=1&pc=1#code/KYDwDg9gTgLgBDAnmYcCSBnAoiAhgYxgB4AVAGjgFUA+OAXjgG0SBdOUGYAOwBMMnKbAPxwAFI0HsQnXv2bCEUAK6oAXHABmuADYZgASjjqtu4AG4AUKEixNSroQCWELnFwY9sEsmCkpMvkUVOAAfTR09alEAfXUSQwBvAF8LC21geAAjCAhtenDTSz0YEkcAW2AIJRhRUUM6WgS4bNz8mGVzOCSKAEZ9SwtcAHdcR3guYCG4AAUoCDLHPVEoYAxcgDdUBrhi0oqqmrr6WhW17U3Rex5gDUcJnn1egAYn-X7Bj2AvHyJMHAJiEgUBANM0ctoKCZIlEoQZLEA

πŸ’» Code

export type IsExact<T, U> = [T] extends [U] ? ([U] extends [T] ? true : false) : false;
export function assertType<T extends true | false>(_: T) {}

let bool = false;
setTimeout(() => { bool = true; }, 1);

await new Promise(resolve => setTimeout(() => resolve(undefined), 100));
assertType<IsExact<typeof bool, false>>(false);

πŸ™ Actual behavior

Argument of type 'false' is not assignable to parameter of type 'true'. In this case, that means that bool is narrowed to false on line 8.

πŸ™‚ Expected behavior

The type of bool should be boolean because it will be true if the timer on line 5 ran and false if it did not. (In practice, with this exact code snippet, it is always true, so it definitely should not be narrowed to false!)

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

@TAGraves
Copy link
Author

Thank you -- I apologize for missing that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants