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

Checking for existence of property in if block when --noUncheckedIndexedAccess, does not work when properties are in an array #52653

Closed
LowArmour opened this issue Feb 7, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@LowArmour
Copy link

LowArmour commented Feb 7, 2023

Bug Report

When --noUncheckedIndexedAccess is activated, this blog from Microsoft Devs states that you can check if the property exists either by using "!" or within an "if" block: https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#no-unchecked-indexed-access

Checking if the property exists by using an if block does not work if the property is an element of an array over which you iterate.

This is relevant when you cannot use the "!" operator, for example the following eslint rule is set to "error": https://typescript-eslint.io/rules/no-non-null-assertion/.

🔎 Search Terms

--noUncheckedIndexedAccess bugs

🕗 Version & Regression Information

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

⏯ Playground Link

Playground link with relevant code

💻 Code

type B = {
    [key: string]: number
}

const obj: B = {
    a: 1,
}
const propA = 'a'
if (obj[propA]) {
    const sum = 1 + obj[propA]  //does not show the error
    console.log(sum)
}
const props = ['a', 'b', 'c', 'd', 'e']
for (const prop of props) {
    if (obj[prop]) {
        const sum = 1 + obj[prop] //does show the error: Object is possibly 'undefined'
        console.log(sum)
    }
}

🙁 Actual behavior

Having --noUncheckedIndexedAccess flag activated and using if blocks to check if an object has a property, when such property is an element of an array over which you iterate does not work.

🙂 Expected behavior

The above should work also when the property is an element of an array over which you iterate.

@MartinJohns
Copy link
Contributor

Duplicate of #10530. Type narrowing does not occur for indexed access forms e[k] where k is not a literal. Just store it in a local const variable.

@LowArmour LowArmour changed the title Checking for existence of property in if block when --noUncheckedIndexedAccess does not work when properties are in an array Checking for existence of property in if block when --noUncheckedIndexedAccess, does not work when properties are in an array Feb 7, 2023
@LowArmour
Copy link
Author

Now I understand. Thanks for help!

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Feb 10, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

4 participants