noConstAssign
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/correctness/noConstAssign
Since: v1.0.0
Sources:
- Same as: 
no-const-assign 
Description
Section titled DescriptionPrevents from having const variables being re-assigned.
Trying to assign a value to a const will cause an TypeError when the code is executed.
Examples
Section titled ExamplesInvalid
Section titled Invalidconst a = 1;a = 4;code-block.js:2:1 lint/correctness/noConstAssign  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ✖ Can’t assign a because it’s a constant
  
    1 │ const a = 1;
  > 2 │ a = 4;
      │ ^
    3 │ 
  
  ℹ This is where the variable is defined as constant
  
  > 1 │ const a = 1;
      │       ^
    2 │ a = 4;
    3 │ 
  
  ℹ Unsafe fix: Replace const with let if you assign it to a new value.
  
    1   │ - const·a·=·1;
      1 │ + let·a·=·1;
    2 2 │   a = 4;
    3 3 │   
  
const a = 2;a += 1;code-block.js:2:1 lint/correctness/noConstAssign  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ✖ Can’t assign a because it’s a constant
  
    1 │ const a = 2;
  > 2 │ a += 1;
      │ ^
    3 │ 
  
  ℹ This is where the variable is defined as constant
  
  > 1 │ const a = 2;
      │       ^
    2 │ a += 1;
    3 │ 
  
  ℹ Unsafe fix: Replace const with let if you assign it to a new value.
  
    1   │ - const·a·=·2;
      1 │ + let·a·=·2;
    2 2 │   a += 1;
    3 3 │   
  
const a = 1;++a;code-block.js:2:3 lint/correctness/noConstAssign  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ✖ Can’t assign a because it’s a constant
  
    1 │ const a = 1;
  > 2 │ ++a;
      │   ^
    3 │ 
  
  ℹ This is where the variable is defined as constant
  
  > 1 │ const a = 1;
      │       ^
    2 │ ++a;
    3 │ 
  
  ℹ Unsafe fix: Replace const with let if you assign it to a new value.
  
    1   │ - const·a·=·1;
      1 │ + let·a·=·1;
    2 2 │   ++a;
    3 3 │   
  
const a = 1, b = 2;
a = 2;code-block.js:3:1 lint/correctness/noConstAssign  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ✖ Can’t assign a because it’s a constant
  
    1 │ const a = 1, b = 2;
    2 │ 
  > 3 │ a = 2;
      │ ^
    4 │ 
  
  ℹ This is where the variable is defined as constant
  
  > 1 │ const a = 1, b = 2;
      │       ^
    2 │ 
    3 │ a = 2;
  
  ℹ Unsafe fix: Replace const with let if you assign it to a new value.
  
    1   │ - const·a·=·1,·b·=·2;
      1 │ + let·a·=·1,·b·=·2;
    2 2 │   
    3 3 │   a = 2;
  
Valid
Section titled Validconst a = 10;let b = 10;b = 20;How to configure
Section titled How to configure{  "linter": {    "rules": {      "correctness": {        "noConstAssign": "error"      }    }  }}