Skip to content

noUncheckedIndexedAccess: not working as expected in "for...of" loopsΒ #42622

Description

@Znerole

Bug Report

πŸ”Ž Search Terms

  • noUncheckedIndexedAccess
  • for...of

πŸ•— Version & Regression Information

  • typescript@4.1.2
  • typescript@4.2.0-dev.20210203

⏯ Playground Link

Playground Link

πŸ’» Code

// this example requires "noUncheckedIndexedAccess" to be enabled to make sensefunctionsomeNumberFunction(_: number){}constmyArray: number[]=[]myArray[5]=5for(leti=0;i<myArray.length;++i){constv=myArray[i]// v is "number | undefined"...if(v!=null){// ... so it needs to be checked for undefinedsomeNumberFunction(v)}}for(constvofmyArray){// v is "number" according to TypeScript...// ... so we can erroneously call the function without checking for undefinedsomeNumberFunction(v)}

πŸ™ Actual behavior

With noUncheckedIndexedAccess enabled, value v in the for...of loop is still treated as number, allowing to unwittingly further process an undefined value.

πŸ™‚ Expected behavior

Value v should be treated as number | undefined to be consistent with index access on arrays.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions