Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Rule suggestion: disallow modifying function parameters #3033

Closed
benny-medflyt opened this issue Jul 12, 2017 · 1 comment · Fixed by #3045
Closed

Rule suggestion: disallow modifying function parameters #3033

benny-medflyt opened this issue Jul 12, 2017 · 1 comment · Fixed by #3045

Comments

@benny-medflyt
Copy link

There is an argument that using const instead of let makes code more readable, since you can be sure at a glance that a variable will not change it's value. (See prefer-const)

Unfortunately, in TypeScript (or JavaScript) there is no way to declare that a particular function parameter should be const (they are all implicitly let).

For example, the following function modifies the x parameter:

function add(x: number, y: number): number {
    x += y;
    return x;
}

I imagine that a lot of projects would be happy to completely ban all such usages in their code base: all function parameters would behave as if they were const. I believe that this can be implemented as a tslint rule, and I suggest that such a rule be added =]

@ajafff
Copy link
Contributor

ajafff commented Jul 12, 2017

IMO such a rule makes sense. The implementation should not be too different from prefer-const. Maybe we can extract some of the logic into a reusable class or function.
Until it is implemented in tslint core, you can use a third party rule: https://github.com/vrsource/vrsource-tslint-rules#no-param-reassign

Just one question if we want to treat destructured parameters as real parameters? Would be best to make it configurable, I guess.

function foo(bar: number) {} // this is a parameter
function foo([bar, baz]: number[]) {} // technically these are local variables ... do we want to treat them as parameter?

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

Successfully merging a pull request may close this issue.

3 participants