Skip to content

Proposal: New rule for prohibiting usage of mutating array methods #74

@yenbekbay

Description

@yenbekbay

It would be great to have a rule that warns about using mutating array methods without explicitly typing the arrays with ReadonlyArray.

Context

In some cases, it's not desirable to explicitly type arrays with ReadonlyArray. For example, when the type of an array can be automatically inferred:

const makeComplexObj = (...) => ({a: new A(...), b: new B(...)});

const someArr = [makeComplexObj(...), makeComplexObj(...), ...];

vs

const someArr: ReadonlyArray<{
  a: A,
  b: B,
}> = [makeComplexObj(...), makeComplexObj(...), ...];

As you can see, typing such an array explicitly can be a bit cumbersome/verbose.

Proposal

Similar to how the no-object-mutation rule deals with objects, the rule would use type information to detect arrays and warn about using methods such as push, pop, shift and others.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions