• Assignment to property of function parameter no-param-reassign

avatar

Last updated: Mar 7, 2024 Reading time · 3 min

banner

# Table of Contents

  • Disabling the no-param-reassign ESLint rule for a single line
  • Disabling the no-param-reassign ESLint rule for an entire file
  • Disabling the no-param-reassign ESLint rule globally

# Assignment to property of function parameter no-param-reassign

The ESLint error "Assignment to property of function parameter 'X' eslint no-param-reassign" occurs when you try to assign a property to a function parameter.

To solve the error, disable the ESLint rule or create a new object based on the parameter to which you can assign properties.

assignment to property of function parameter eslint no param reassign

Here is an example of how the error occurs.

The ESLint rule forbids assignment to function parameters because modifying a function's parameters also mutates the arguments object and can lead to confusing behavior.

One way to resolve the issue is to create a new object to which you can assign properties.

We used the spread syntax (...) to unpack the properties of the function parameter into a new object to which we can assign properties.

If you need to unpack an array, use the following syntax instead.

The same approach can be used if you simply need to assign the function parameter to a variable so you can mutate it.

We declared the bar variable using the let keyword and set it to the value of the foo parameter.

We are then able to reassign the bar variable without any issues.

# Disabling the no-param-reassign ESLint rule for a single line

You can use a comment if you want to disable the no-param-reassign ESLint rule for a single line.

Make sure to add the comment directly above the assignment that causes the error.

# Disabling the no-param-reassign ESLint rule for an entire file

You can also use a comment to disable the no-param-reassign ESLint rule for an entire file.

Make sure to add the comment at the top of the file or at least above the function in which you reassign parameters.

The same approach can be used to disable the rule only for a single function.

The first comment disables the no-param-reassign rule and the second comment enables it.

If you try to reassign a parameter after the second comment, you will get an ESLint error.

# Disabling the no-param-reassign ESLint rule globally

If you need to disable the no-param-reassign rule globally, you have to edit your .eslintrc.js file.

disable no param reassign rule globally

If you only want to be able to assign properties to an object parameter, set props to false instead of disabling the rule completely.

The following code is valid after making the change.

If you use a .eslintrc or .eslintrc.json file, make sure to double-quote the properties and values.

If you want to only allow assignment to object parameters, use the following line instead.

Make sure all properties are double-quoted and there are no trailing commas if your config is written in JSON.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

  • eslint is not recognized as an internal or external command
  • Plugin "react" was conflicted between package.json » eslint-config-react-app
  • React: Unexpected use of 'X' no-restricted-globals in ESLint
  • TypeScript ESLint: Unsafe assignment of an any value [Fix]
  • ESLint error Unary operator '++' used no-plusplus [Solved]
  • ESLint Prefer default export import/prefer-default-export
  • Arrow function should not return assignment. eslint no-return-assign
  • TypeError: Cannot redefine property: X in JavaScript [Fixed]
  • ESLint: disable multiple rules or a rule for multiple lines
  • Expected linebreaks to be 'LF' but found 'CRLF' linebreak-style
  • Missing return type on function TypeScript ESLint error

book cover

Borislav Hadzhiev

Web Developer

buy me a coffee

Copyright © 2024 Borislav Hadzhiev

Disallow Reassignment of Function Parameters (no-param-reassign)

Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

Rule Details

This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

Examples of incorrect code for this rule:

Examples of correct code for this rule:

This rule takes one option, an object, with a boolean property "props" , and arrays "ignorePropertyModificationsFor" and "ignorePropertyModificationsForRegex" . "props" is false by default. If "props" is set to true , this rule warns against the modification of parameter properties unless they're included in "ignorePropertyModificationsFor" or "ignorePropertyModificationsForRegex" , which is an empty array by default.

Examples of correct code for the default { "props": false } option:

Examples of incorrect code for the { "props": true } option:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsFor" set:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsForRegex" set:

When Not To Use It

If you want to allow assignment to function parameters, then you can safely disable this rule.

Further Reading

  • JavaScript: Don’t Reassign Your Function Arguments

This rule was introduced in ESLint 0.18.0.

  • Rule source
  • Documentation source

© OpenJS Foundation and other contributors Licensed under the MIT License. https://eslint.org/docs/rules/no-param-reassign

no-param-reassign

Disallow reassigning function parameters

Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

Rule Details

This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

Examples of incorrect code for this rule:

Examples of correct code for this rule:

This rule takes one option, an object, with a boolean property "props" , and arrays "ignorePropertyModificationsFor" and "ignorePropertyModificationsForRegex" . "props" is false by default. If "props" is set to true , this rule warns against the modification of parameter properties unless they’re included in "ignorePropertyModificationsFor" or "ignorePropertyModificationsForRegex" , which is an empty array by default.

Examples of correct code for the default { "props": false } option:

Examples of incorrect code for the { "props": true } option:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsFor" set:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsForRegex" set:

When Not To Use It

If you want to allow assignment to function parameters, then you can safely disable this rule.

This rule was introduced in ESLint v0.18.0.

Further Reading

JavaScript: Don’t Reassign Your Function Arguments

  • Rule source
  • Tests source

© OpenJS Foundation and other contributors Licensed under the MIT License. https://eslint.org/docs/latest/rules/no-param-reassign

How to avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript?

  • Post author By John Au-Yeung
  • Post date April 28, 2022
  • No Comments on How to avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript?

Labrador retriever puppy walking on green grass

Sometimes, we want to avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript.

In thiks article, we’ll look at how to avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript.

To avoid no-param-reassign ESLint error when setting a property on a DOM object with JavaScript, we can assign the parameter to a variable.

For instance, we write

to create the f function.

In it, we assign el to the theElement variable.

And then we add the theElement.expand property to it.

Related Posts

In a DOM node object, we see the tagName and nodeName property when we inspect…

There're a few ways to add properties to an object in JavaScript. One way is…

Sometimes, we may want to remove a CSS property from an HTML element using JavaScript.…

eslint assignment to property of function parameter 'state'.(no param reassign)

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

eslint assignment to property of function parameter 'state'.(no param reassign)

Disallow Reassignment of Function Parameters (no-param-reassign)

Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

Rule Details

This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

Examples of incorrect code for this rule:

Examples of correct code for this rule:

This rule takes one option, an object, with a boolean property "props" and an array "ignorePropertyModificationsFor" . "props" is false by default. If "props" is set to true , this rule warns against the modification of parameter properties unless they're included in "ignorePropertyModificationsFor" , which is an empty array by default.

Examples of correct code for the default { "props": false } option:

Examples of incorrect code for the { "props": true } option:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsFor" set:

When Not To Use It

If you want to allow assignment to function parameters, then you can safely disable this rule.

Further Reading

  • JavaScript: Don’t Reassign Your Function Arguments

This rule was introduced in ESLint 0.18.0.

  • Rule source
  • Documentation source

Disallow Reassignment of Function Parameters (no-param-reassign)

Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

Rule Details

This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

Examples of incorrect code for this rule:

Examples of correct code for this rule:

This rule takes one option, an object, with a boolean property "props" and an array "ignorePropertyModificationsFor" . "props" is false by default. If "props" is set to true , this rule warns against the modification of parameter properties unless they’re included in "ignorePropertyModificationsFor" , which is an empty array by default.

Examples of correct code for the default { "props" : false } option:

Examples of incorrect code for the { "props" : true } option:

Examples of correct code for the { "props" : true } option with "ignorePropertyModificationsFor" set:

When Not To Use It

If you want to allow assignment to function parameters, then you can safely disable this rule.

Further Reading

  • JavaScript: Don’t Reassign Your Function Arguments

This rule was introduced in ESLint 0.18.0.

  • Rule source
  • Documentation source

parameter-properties

Require or disallow parameter properties in class constructors.

TypeScript includes a "parameter properties" shorthand for declaring a class constructor parameter and class property in one location. Parameter properties can be confusing to those new to TypeScript as they are less explicit than other ways of declaring and initializing class members.

This rule can be configured to always disallow the use of parameter properties or enforce their usage when possible.

Try this rule in the playground ↗

This rule accepts the following options:

This rule, in its default state, does not require any argument and would completely disallow the use of parameter properties. It may take an options object containing either or both of:

  • "allow" : allowing certain kinds of properties to be ignored
  • "prefer" : either "class-property" (default) or "parameter-property"

If you would like to ignore certain kinds of properties then you may pass an object containing "allow" as an array of any of the following options:

  • readonly , allows readonly parameter properties.
  • private , allows private parameter properties.
  • protected , allows protected parameter properties.
  • public , allows public parameter properties.
  • private readonly , allows private readonly parameter properties.
  • protected readonly , allows protected readonly parameter properties.
  • public readonly , allows public readonly parameter properties.

For example, to ignore public properties:

By default, the rule prefers class property ( "class-property" ). You can switch it to instead preferring parameter property with ( "parameter-property" ).

In "parameter-property" mode, the rule will issue a report when:

  • A class property and constructor parameter have the same name and type
  • The constructor parameter is assigned to the class property at the beginning of the constructor

Examples of code for this rule with no options at all:

  • ❌ Incorrect

Examples of code for the { "allow": ["readonly"] } options:

Examples of code for the { "allow": ["private"] } options:

protected ​

Examples of code for the { "allow": ["protected"] } options:

Examples of code for the { "allow": ["public"] } options:

private readonly ​

Examples of code for the { "allow": ["private readonly"] } options:

protected readonly ​

Examples of code for the { "allow": ["protected readonly"] } options:

public readonly ​

Examples of code for the { "allow": ["public readonly"] } options:

"parameter-property" ​

Examples of code for the { "prefer": "parameter-property" } option:

When Not To Use It ​

If you don't care about which style of parameter properties in constructors is used in your classes, then you will not need this rule.

However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.

Resources ​

  • Rule source
  • Test source
  • private readonly
  • protected readonly
  • public readonly
  • "parameter-property"
  • When Not To Use It

no-param-reassign

Disallow reassigning function parameters

对作为函数参数声明的变量进行赋值可能会产生误导并导致混乱的行为,因为修改函数参数也会改变 arguments 对象。通常情况下,对函数参数的赋值是无意的,表明了一个错误或程序员的错误。

这条规则也可以被配置为在修改函数参数时失败。参数的副作用会导致反直觉的执行流程,使错误难以追踪。

这条规则的目的是防止因修改或重新分配函数参数而引起的非预期行为。

使用此规则的 错误 示例:

使用此规则的 正确 示例:

这个规则有一个选项,是一个对象,有一个布尔属性 "props" 和数组 "ignorePropertyModificationsFor" 和 "ignorePropertyModificationsForRegex" 。 "props" 默认为 false 。如果 "props" 设置为 true ,本规则警告不要修改参数属性,除非它们被包含在 "ignorePropertyModificationsFor" 或 "ignorePropertyModificationsForRegex" 中,默认为空数组。

使用默认的 { "props": false } 选项的 正确 示例:

使用 { "props": true } 选项的 错误 示例:

设置了 "ignorePropertyModificationsFor" 的 { "props": true } 选项的**正确的代码示例:

设置了 "ignorePropertyModificationsForRegex" 的 { "props": true } 选项的**正确的代码示例:

如果你想允许对函数参数进行赋值,你可以安全地禁用此规则。

This rule was introduced in ESLint v0.18.0.

Further Reading

Avatar image for spin.atomicobject.com

  • Rule source
  • Tests source

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

no-param-reassign error is set for @reduxjs/toolkit createSlice/createReducer #14759

@OldMeM

OldMeM Jun 30, 2021

Beta Was this translation helpful? Give feedback.

I found an answer to this on futher searching.

  • reduxjs/redux-toolkit#521
  • Linting State Mutations

I should have searched for this first in their repo before coming over to eslint. 🤦‍♀️

Replies: 3 comments

Oldmem jul 2, 2021 author, nzakas jul 3, 2021 maintainer, oldmem jul 9, 2021 author.

@OldMeM

  • Numbered list
  • Unordered list
  • Attach files

Select a reply

Disallow Reassignment of Function Parameters (no-param-reassign)

Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

Rule Details

This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.

Examples of incorrect code for this rule:

Examples of correct code for this rule:

This rule takes one option, an object, with a boolean property "props" and an array "ignorePropertyModificationsFor" . "props" is false by default. If "props" is set to true , this rule warns against the modification of parameter properties unless they’re included in "ignorePropertyModificationsFor" , which is an empty array by default.

Examples of correct code for the default { "props": false } option:

Examples of incorrect code for the { "props": true } option:

Examples of correct code for the { "props": true } option with "ignorePropertyModificationsFor" set:

When Not To Use It

If you want to allow assignment to function parameters, then you can safely disable this rule.

Further Reading

  • JavaScript: Don’t Reassign Your Function Arguments

This rule was introduced in ESLint 0.18.0.

  • Rule source
  • Documentation source

IMAGES

  1. How to fix this ESlint no param reassign error in my Filter

    eslint assignment to property of function parameter 'state'.(no param reassign)

  2. no-param-reassign

    eslint assignment to property of function parameter 'state'.(no param reassign)

  3. javascript

    eslint assignment to property of function parameter 'state'.(no param reassign)

  4. Assignment to property of function parameter no-param-reassign

    eslint assignment to property of function parameter 'state'.(no param reassign)

  5. Assignment to property of function parameter no-param-reassign

    eslint assignment to property of function parameter 'state'.(no param reassign)

  6. 项目集成eslint后检查错误,持续更新

    eslint assignment to property of function parameter 'state'.(no param reassign)

VIDEO

  1. Sum Numbers Using Function

  2. How to use Contour Tool with Full Property Function in CorelDraw X-7,6,5,4,3 |Hindi/Urdu| # 19

  3. Use Destructuring Assignment to Pass an Object as a Function's Parameters (ES6) freeCodeCamp

  4. How to use Blend Tool with Complete Property Function in CorelDraw X-7,6,5,4,3 |Hindi/Urdu| # 20

  5. 06 Set Default Parameters for Your Functions

  6. ES6: Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements

COMMENTS

  1. javascript

    The no-param-reassign warning makes sense for common functions, but for a classic Array.forEach loop over an array which you intend to mutate it isn't to appropriate. However, to get around this, you can also use Array.map with a new object (if you are like me, dislike snoozing warnings with comments): someArray = someArray.map((_item) => {.

  2. no-param-reassign

    If you want to allow assignment to function parameters, then you can safely disable this rule. Strict mode code doesn't sync indices of the arguments object with each parameter binding. Therefore, this rule is not necessary to protect against arguments object mutation in ESM modules or other strict mode functions. Version

  3. Assignment to property of function parameter (no-param-reassign)

    10. This is a common ESLint issue that appears frequently on old codebase. You have modified the result variable which was passed as parameter. This behavior is prohibited by the rule. To resolve it, copy the argument to a temporary variable and work on it instead: export const fn = article => article.categoryValueDtoSet.reduce((res, item) => {.

  4. Assignment to property of function parameter no-param-reassign

    function createEmployee(emp) { // ⛔️ Assignment to property of function parameter 'emp'. eslint no-param-reassign. emp.name = 'bobby hadz'; emp.salary = 500; return emp; } The ESLint rule forbids assignment to function parameters because modifying a function's parameters also mutates the arguments object and can lead to confusing behavior.

  5. no-param-reassign

    A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.

  6. no-param-reassign

    Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. ... Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared ...

  7. No-param-reassign

    Rule Details. This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters. Examples of incorrect code for this rule: /*eslint no-param-reassign: "error"*/ function foo(bar) {. bar = 13; } function foo(bar) {. bar++; } function foo(bar) { for (bar in baz) {} } function foo(bar) { for (bar of baz) {} }

  8. How to avoid no-param-reassign ESLint error when setting a property on

    Spread the love Related Posts What's the Difference Between the tagName and nodeName Property of a DOM Node in JavaScript?In a DOM node object, we see the tagName and nodeName property when we inspect… How to Add Property to an Object in JavascriptThere're a few ways to add properties to an object in JavaScript. One […]

  9. Do we want to recommend the no-param-reassign eslint rule in ...

    I just had this idea when another user tried to assign to the state function argument. There is actually an eslint rule for that: no-param-reassign. I'd suggest we either add that rule as a recommendation somehwere in the docs or go even one step farther and create a shareable config package .. That way we could add some more recommended rules in the future.

  10. no-param-reassign

    Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared as function parameters can be misleading and lead to confusing ... /*eslint no-param-reassign: "error"*/ function foo ... an object, with a boolean property "props" and an array "ignorePropertyModificationsFor". "props" is false by default. If ...

  11. TypeScript Reactjs: Assignment to property of function parameter 'state

    Hello I have a problem in my estlint: Assignment to property of function parameter 'state'. eslintno-param-reassign on this code: state.sideisOpen = action.payload; interface SideBar { sideisOpen: boolean; } const INITIAL_STATE: SideBar ...

  12. Rule Change: Add option to no-param-reassign to allow ...

    I know that there is an option called ignorePropertyModificationsFor which allows properties of params with certain names to be modified, but this request is different. I'm requesting an option which would allow the param itself to be reassigned if it has a certain name. This would be useful for people who want to avoid param reassignment, but who want to allow it when using something like ...

  13. no-param-reassign

    Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared as function parameters can be misleading and lead to confusing ... /*eslint no-param-reassign: "error"*/ function foo ... with a boolean property "props" and an array "ignorePropertyModificationsFor". "props" is false by default. If "props" is set ...

  14. parameter-properties

    TypeScript includes a "parameter properties" shorthand for declaring a class constructor parameter and class property in one location. Parameter properties can be confusing to those new to TypeScript as they are less explicit than other ways of declaring and initializing class members. This rule can be configured to always disallow the use of ...

  15. no-param-reassign

    对作为函数参数声明的变量进行赋值可能会产生误导并导致混乱的行为,因为修改函数参数也会改变 arguments 对象。 通常情况下,对函数参数的赋值是无意的,表明了一个错误或程序员的错误。

  16. no-param-reassign error is set for @reduxjs/toolkit createSlice

    You can only write "mutating" logic in Redux Toolkit's createSlice and createReducer because they use Immer inside! If you write mutating logic in reducers without Immer, it will mutate the state and cause bugs! Is it possible to change eslint config for no-param-reassign rule to ignore the any code within createSlice or createReducer only?

  17. no-param-reassign

    Disallow Reassignment of Function Parameters (no-param-reassign) 禁止对函数参数再赋值 (no-param-reassign) Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is ...

  18. How to resolve no-param-reassign error in a function

    Normally you don't want to assign to a property of the parameter. It mutates the passed parameter which most user don't expect to happen. In your scenario this is most likely not much of a problem, assuming prepareCandidate returns a freshly created object. This would mean you could add a comment to disable the warning for this particular piece of code.

  19. no-param-reassign

    Disallow Reassignment of Function Parameters (no-param-reassign) Assignment to variables declared as function parameters can be misleading and lead to confusing ... /*eslint no-param-reassign: "error"*/ function foo ... with a boolean property "props" and an array "ignorePropertyModificationsFor". "props" is false by default. If "props" is set ...

  20. Fixing no-param-reassign Eslint issue in function

    yes exactly. or just a simply change the parameter name to something else and set constvariable name as recurrence. so now you don't have to replace everywhere. - Nazeer Sep 23, 2020 at 8:00