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

Maybe false positive from TS2801 (Promise<...> appears to always be defined) #45608

Closed
plantain-00 opened this issue Aug 28, 2021 · 2 comments
Closed

Comments

@plantain-00
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

TS2801

πŸ•— Version & Regression Information

  • This changed between versions 4.4.2 and 4.3.5

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const a: { [key: string]: Promise<string> } = {}
if (a['a']) {
  
}

πŸ™ Actual behavior

This condition will always return true since this 'Promise' is always defined.

πŸ™‚ Expected behavior

No error.

@MartinJohns
Copy link
Contributor

You want to enable noUncheckedIndexedAccess. Without this compiler flag it's assumed that indexed access returns a valid value.

@Alexsey
Copy link

Alexsey commented Mar 3, 2023

Unfortunately, due to #10530 noUncheckedIndexedAccess is not handy here:

if (a['a']) {
  a['a'] // Promise<string> | undefined
}

And I assume it will be even harder to narrow for index value 'a' to be variable and not literal

So, the workaround here is to use in instead of index access:

if ('a' in a) {
  a['a'] // Promise<string>
}

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

3 participants