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

variable incorrectly typed as true instead of boolean when change is made inside IIFE #58452

Closed
mdouglass opened this issue May 6, 2024 · 1 comment

Comments

@mdouglass
Copy link

πŸ”Ž Search Terms

type inference boolean

πŸ•— Version & Regression Information

TypeScript 5.4.5

⏯ Playground Link

https://typescript-eslint.io/play/#ts=5.4.3&fileType=.ts&code=IYZwngdgxgBAZgV2gFwJYHsIwLbFRACgEoAuGABQCd1tUQBTAHgDd1UATAPhgG8AoGDAA29ZDGbAhCejAC8MZJWkCYwAO54xBUJFjE53foMESpM%2BXEkMVAXyLEVqODAKnpRXisFRMIdCIA6IXQAcwIAcjcZOgUlenCiWxh6IQZPYxgfCD9A4LDIyWkYGMtU%2BMTBGz4qvlYOHDxCRKA&eslintrc=N4KABGBECmAeAu0B2ATAzpAXGA2jNANgJZLyYBO0AxgPYC2dyK0KkANFAA4ECuA5iUwABeAE9O0NFXJFO8ALSTipAPRp4MqgrET5VABbUA1i0gBdEAF8QQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

πŸ’» Code

async function main(): Promise<void> {
  let value = true
  await (async () => {
    value = false
  })()
  if (value) {
    console.log('value is true')
  } else {
    console.log('value is false')
  }
}

void main()

πŸ™ Actual behavior

I originally reported this as a bug in typescript-eslint but was told that the underlying type deduction was incorrect in this case and therefore it wasn't a bug there.

The variable value in this code is deduced as type true (seen via hover in vscode on value in the if statement) but would be correctly typed as boolean since it can be (and in this code sample is) changed. If you run the code it logs value is false despite the type being true.

I have a slightly simpler playground that does not involve an async function here: https://www.typescriptlang.org/play/?ts=5.4.5#code/DYUwLgBAbghsCuIIF4IDM4GcQG4BQA2gORoD2pRAugHRkBOAojAMYAWAFO2CAB5gCUKAHwQA3ngjQ4iFBDB1E+AL798ASzQR2sBCEHjJzUgDtMpUNWCkA5uyI6ZazHIUgiqvEoghg2MRIgjU3MQSxs7ByQndCw3DyUgA

πŸ™‚ Expected behavior

value should have the type boolean

Additional information about the issue

xref typescript-eslint/typescript-eslint#9055

@mdouglass
Copy link
Author

clsoing after pointed at #9998

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

1 participant