• 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

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

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

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

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)

禁止对函数参数再赋值 (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.

对函数参数中的变量进行赋值可能会误导读者,导致混乱,也会改变 arguments 对象。通常,对函数参数进行赋值并非有意为之,更多的是程序员的书写错误做成的。

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.

该规则有一个选项,是个对象,其中有一个 "props" 的布尔属性和一个数组属性 "ignorePropertyModificationsFor" 。 "props" 默认为 false 。如果 "props" 设置为 true ,对参数的任何属性的修改,该规则都将发出警告, 除非在 "ignorePropertyModificationsFor" (默认为空数组) 有该参数。

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

默认选项 { "props": false } 的 正确 代码示例:

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

选项 { "props": true } 的 错误 代码示例:

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

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

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.

该规则在 ESLint 0.18.0 中被引入。

  • Rule source
  • Documentation source

JavaScript の罠を回避! ESLint ルール "no-param-reassign" で関数のパラメータ変更を検知

Eslint ルール "no-param-reassign" の解説.

no-param-reassign ルールは、デフォルトで有効になっています。以下の方法で、このルールの設定を変更することができます。

  • .eslintrc.json ファイルに以下の設定を追加します。
  • 上記の設定例では、 props オプションを false に設定することで、オブジェクトパラメータのプロパティの変更を許可しています。
  • このルールは、関数のパラメータへの直接的な再割り当てのみを検知します。間接的な再割り当ては検知されません。
  • オブジェクトパラメータのプロパティを変更する場合、 props オプションを true に設定することで、警告を抑制することができます。
  • デフォルト設定では、 arguments オブジェクトへのアクセスも警告されます。

このルールが違反された場合、以下のエラーメッセージが表示されます。

no-param-reassign ルールは、関数のパラメータへの再割り当てによるバグを防ぐために役立ちます。このルールの使用方法と注意点、サンプルコードを理解し、コードの品質向上に役立ててください。

function パラメータの再割り当てを禁止する

関数パラメータとして宣言された変数への代入は誤解を招き、混乱を招く動作を引き起こす可能性があります。これは、関数パラメータを変更すると、厳密モードでない場合に arguments オブジェクトも変更されるためです (下記の When Not To Use It を参照)。多くの場合、関数パラメータへの割り当ては意図せず、間違いまたはプログラマ エラーを示します。

このルールは、関数パラメーターが変更されたときに失敗するように構成することもできます。パラメーターの副作用により、直感に反する実行フローが発生し、エラーの追跡が困難になる可能性があります。

Rule Details

このルールは、関数パラメータの変更または再割り当てによって引き起こされる意図しない動作を防ぐことを目的としています。

このルールの間違ったコードの例:

このルールの正しいコードの例:

このルールは、 boolean プロパティ "props" と配列 "ignorePropertyModificationsFor" および "ignorePropertyModificationsForRegex" を持つオブジェクトという 1 つのオプションを取ります。デフォルトでは "props" は false です。 "props" が true に設定されている場合、このルールは、デフォルトで空の配列である "ignorePropertyModificationsFor" または "ignorePropertyModificationsForRegex" にパラメータ プロパティが含まれていない限り、パラメータ プロパティの変更に対して警告します。

デフォルトの { "props": false } オプションの正しいコードの例:

{ "props": true } オプションの間違ったコードの例:

"ignorePropertyModificationsFor" が設定された { "props": true } オプションの正しいコードの例:

"ignorePropertyModificationsForRegex" が設定された { "props": true } オプションの正しいコードの例:

関数パラメーターへの割り当てを許可する場合は、このルールを安全に無効にすることができます。

厳密モードのコードは、引数オブジェクトのインデックスを各パラメーター バインディングと同期しません。したがって、このルールは、ESM モジュールまたは他の厳密モード関数での引数オブ​​ジェクトの変更から保護するためには必要ありません。

このルールは ESLint v0.18.0 で導入されました。

Further Reading

JavaScript: 関数の引数を再割り当てしないでください

  • Rule source
  • Tests source

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

ESLint ルール no-object-constructor とは?

このルールを有効にするには、.eslintrc ファイルに以下の設定を追加します。以下のコードは、no-object-constructor ルールに違反します。Object コンストラクタに引数を渡す場合は、このルールは違反しません。オブジェクトリテラル記法では、プロパティ名のキーの前に

  • JavaScript コードにおける "javascript:" スキームの安全な使用法
  • ESLint ルール array-callback-return を無効にする場合の注意点
  • 従来の関数式はもう古い? ESLint ルール "prefer-arrow-callback" でアロー関数を使いこなそう

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

ワンランク上のJavaScriptコードへ!ESLint no-regex-spaces ルールで正規表現をスッキリさせよう

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

ESLint func-style ルールを使いこなして、コードの読みやすさを向上させよう

不要なコンストラクタを検知してコードを簡潔に! eslint ルール "no-useless-constructor" の徹底解説.

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

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

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

IMAGES

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

    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. 解决Vue、vuex报“Assignment to property of function parameter ‘state‘” 的方法

    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. javascript

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

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

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

VIDEO

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

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

  3. #16

  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. Intellectual Property Week 6 Quiz Assignment Solution

COMMENTS

  1. 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

  2. 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) => {.

  3. 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.

  4. no-param-reassign

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

  5. 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 ...

  6. 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) {} }

  7. ESLint: Assignment to property of function parameter

    I am getting linting error: Assignment to property of function parameter. How can I resolve that, barring disabling the linting rule? I keep seeing array destructuring as the answer to this problem, however I'm not too sure what that would look like in practice given that this is a pretty complex structure.

  8. 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 ...

  9. 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 ...

  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. Do we want to recommend the `no-param-reassign` eslint rule in the docs

    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.

  12. no-param-reassign

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

  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. Assignment to property of function parameter 'a' Eslint

    There's no need to reassign the pos property of the function arguments (a, b). You should just assign a new variable (aPos, bPos), which is a best practice, hence why ESLint is complaining. You should avoid unnecessary mutations/side-effects whenever possible in your code to prevent bugs and create an overall more efficient program architecture.

  15. 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 ...

  16. no-param-reassign: function type exceptions #6339

    eslint output: Assignment to function parameter 'line' no-param-reassign. In such cases creation an intermediate variable could be overcomplication. May be some additions to the rule like proxyTraps: false, eventListeners: false would be helpful.

  17. 优雅解决: assignment to property of function parameter 'state'

    优雅解决: assignment to property of function parameter 'state'. 在airbnb的eslint规则中,有这样一条规则 no-param-reassign. 目的是提醒你不要直接修改函数的入参。. 因为假如入参是一个对象,修改入参可能会导致对象的属性被覆盖。. obj.key = 1; // 可能对象本身就用key的属性 ...

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

    You can assign recurrence to a const variable in side the function and use that in side the function. onUpdateRecurrence = (recurrence) => { const recurrenceValue = {...recurrence}; //Your Code }

  19. JavaScript の罠を回避! ESLint ルール "no-param-reassign" で関数のパラメータ変更を検知

    Assignment to function parameter 'bar' no-param-reassign ルールは、関数のパラメータへの再割り当てによるバグを防ぐために役立ちます。このルールの使用方法と注意点、サンプルコードを理解し、コードの品質向上に役立ててください。

  20. 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?

  21. Assignment to function parameter 'value' no-param-reassign

    Assignment to property of function parameter 'a' Eslint. ... Assignment to property of function parameter (no-param-reassign) 2 ... Argument expression expected. eslint" with Typescript map function in React. 0 Fixing no-param-reassign Eslint issue in function. 6 ...