pageClass | sidebarDepth | title | description |
---|---|---|---|
rule-details |
0 |
svelte/prefer-const |
Require `const` declarations for variables that are never reassigned after declared |
Require
const
declarations for variables that are never reassigned after declared
- ❗ This rule has not been released yet.
- 🔧 The
--fix
option on the command line can automatically fix some of the problems reported by this rule.
This rule reports the same as the base ESLint prefer-const
rule, except that ignores Svelte reactive values such as $state
, $derived
and $props
. If this rule is active, make sure to disable the base prefer-const
rule, as it will conflict with this rule.
<script>
/* eslint svelte/prefer-const: "error" */
// ✓ GOOD
const { a, b } = $props();
let { a, b } = $state();
// ✗ BAD
// Imagine obj is not re-assigned, therefore it should be constant
let obj = { a, b };
</script>
{
"svelte/prefer-const": [
"error",
{
"destructuring": "any",
"ignoreReadonly": true
}
]
}
destructuring
: The kind of the way to address variables in destructuring. There are 2 values:any
(default): if any variables in destructuring should be const, this rule warns for those variables.all
: if all variables in destructuring should be const, this rule warns the variables. Otherwise, ignores them.
ignoreReadonly
: Iftrue
, this rule will ignore variables that are read between the declaration and the first assignment.
- See ESLint
prefer-const
rule for more information about the base rule.
Taken with ❤️ from ESLint core