# svelte/prefer-const
Require
const
declarations for variables that are never reassigned after declared
- ๐ง The
--fix
option on the command line can automatically fix some of the problems reported by this rule.
# ๐ Rule Details
This rule reports the same as the base ESLint prefer-const
rule, except that ignores Svelte reactive values such as $derived
and $props
as default. 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 c = $state('');
let d = $derived(a * 2);
let e = $derived.by(() => b * 2);
// โ BAD
let 'obj' is never reassigned. Use 'const' instead. (svelte/prefer-const)obj = { a, b };
let 'g' is never reassigned. Use 'const' instead. (svelte/prefer-const)g = $state(0);
let 'h' is never reassigned. Use 'const' instead. (svelte/prefer-const)h = $state({ count: 1 });
</script>
<input bind:value={c} />
<input bind:value={h.count} />
# ๐ง Options
{
"svelte/prefer-const": [
"error",
{
"destructuring": "any",
"additionalProperties": false,
"excludedRunes": ["$props", "$derived"]
}
]
}
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.
ignoreReadBeforeAssign
: This is an option to avoid conflicting withno-use-before-define
rule (without โnofuncโ option). Iftrue
is specified, this rule will ignore variables that are read between the declaration and the first assignment. Default isfalse
.excludedRunes
: An array of rune names that should be ignored. Even if a rune is declared withlet
, it will still be ignored.
# ๐ Further Reading
- See ESLint prefer-const rule for more information about the base rule.
# ๐ Version
This rule was introduced in eslint-plugin-svelte v3.0.0-next.6
# ๐ Implementation
Taken with โค๏ธ from ESLint core