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

noUncheckedIndexedAccess and checked bracket notation access #43062

Closed
PSeitz opened this issue Mar 3, 2021 · 2 comments
Closed

noUncheckedIndexedAccess and checked bracket notation access #43062

PSeitz opened this issue Mar 3, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@PSeitz
Copy link

PSeitz commented Mar 3, 2021

Bug Report

πŸ”Ž Search Terms

noUncheckedIndexedAccess bracket access undefined

πŸ•— Version & Regression Information

  • I was unable to test this on prior versions because noUncheckedIndexedAccess is available since 4.1

⏯ Playground Link

// @noUncheckedIndexedAccess
const meObj: { [propName: string]: string } = {}
const meKey: keyof typeof meObj = 'meh'
if (meObj[meKey]){
  meObj[meKey].toString() // Object is possibly 'undefined'.
}

Workbench Repro

πŸ™ Actual behavior

Compile Error: Object is possibly 'undefined'. error

πŸ™‚ Expected behavior

Typescript should be able to recognize the existence of the property with a check. This generates a lot of wrong errors in our codebase when the noUncheckedIndexedAccess compiler flag is enabled.

@MartinJohns
Copy link
Contributor

Duplicate of #10530. Type narrowing does not occur for indexed access forms e[k] where k is not a literal.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 3, 2021
@PSeitz
Copy link
Author

PSeitz commented Mar 4, 2021

This one also has similar examples further below #41612
I'll close this one then.

@PSeitz PSeitz closed this as completed Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants