-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Unexpected "Object is possibly 'undefined'." error with a nullable key #54854
Comments
Duplicate of #10530. Type narrowing does not occur for indexed access forms |
Apologies for the duplicate. I suspected it was going to be a long-standing issue. If this a common enough problem, please consider adding it to the FAQ if it fits there. Here's the fix as suggested (and with the playground) if (randomKey) {
const arr = food[randomKey];
if (arr) {
arr.push("whatever")
} else {
food[randomKey] = ["whatever"];
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
🔎 Search Terms
Object is possibly 'undefined' / undefined key / nullable key
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code (Nightly)
💻 Code
🙁 Actual behavior
It fails to compile with an error:
Object is possibly 'undefined'.(2532)
.It should compile because:
randomKey
is truthy (I triedif (randomKey !== undefined) {
but got the same error)food[randomKey]
is truthy (I triedif (food[randomKey] !== undefined) {
but got the same error)🙂 Expected behavior
It should compile without errors.
The text was updated successfully, but these errors were encountered: