-
Notifications
You must be signed in to change notification settings - Fork 14
Closed
Labels
Description
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.