Skip to content

unicorn/no-array-for-each - should allow forEach when the index argument is used #23991

Description

@DetachHead
declare const foo: string[];

declare const doThingWithIndex: (index: number) => void;

foo.forEach((value, index) => { // error: unicorn/no-array-for-each
  console.log(value);
  doThingWithIndex(index);
});

there's no good way to convert this to a for loop:

for (const index in foo) { // now triggers new errors: guard-for-in, typescript/no-for-in-array
  const value = foo[index]
  doThingWithIndex(index); // type error, because index is a string
}

Metadata

Metadata

Assignees

Labels

Fields

Priority

None yet

Start date

None yet

Target date

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions