script/prefer-computed
Prefer computed() for derived reactive state.
| Default severity | Presets | Automatic fix | Options |
|---|---|---|---|
warning |
None | No | None |
Applies to: JS/TS scripts in Vue SFCs; examples show the relevant Options API or script setup form.
The watcher must only derive the destination. Editable copies and callbacks with other side effects are allowed.
Bad
The watcher only copies a derivation of count into a second ref, doubled, so the derived state is maintained through manual synchronization.
<script setup lang="ts">
import { ref, watch } from "vue";
const count = ref(0);
const doubled = ref(0);
watch(count, (value) => { doubled.value = value * 2; });
</script>
Good
computed(() => count.value * 2) expresses the derivation directly and removes both the extra writable ref and its synchronization watcher.
<script setup lang="ts">
import { ref, computed } from "vue";
const count = ref(0);
const doubled = computed(() => count.value * 2);
</script>
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"script/prefer-computed": "warn"
}
},
});
Then run vp run lint.