Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
yenbekbay opened this issue Feb 14, 2018 · 2 comments
Closed

Comments

@yenbekbay
Copy link

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.

@yenbekbay yenbekbay changed the title Proposal: new rule for prohibiting usage of mutable array methods Proposal: New rule for prohibiting usage of mutable array methods Feb 14, 2018
@yenbekbay yenbekbay changed the title Proposal: New rule for prohibiting usage of mutable array methods Proposal: New rule for prohibiting usage of mutating array methods Feb 14, 2018
@jonaskello
Copy link
Owner

This sounds like a nice complement to the no-object-mutation rule, something likeno-array-mutation. I would be happy to take a PR for it, or I'll look into it when I have some spare time.

@dalen
Copy link

dalen commented Jun 13, 2018

This is great, especially considering typing as ReadonlyArray causes microsoft/TypeScript#15972

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

No branches or pull requests

3 participants