You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeB={[key: string]: number}constobj: B={a: 1,}constpropA='a'if(obj[propA]){constsum=1+obj[propA]//does not show the errorconsole.log(sum)}constprops=['a','b','c','d','e']for(constpropofprops){if(obj[prop]){constsum=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.
The text was updated successfully, but these errors were encountered:
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
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
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
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 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.
The text was updated successfully, but these errors were encountered: