Reactivity mistakes
Vue's trickiest reactivity bugs compile cleanly, run without throwing, and silently freeze updates or evaluate wrong conditions. SAFE-TS catches them statically across components and composables.
In short
- Never forget
.valuein script: a barerefis always truthy and stringifies as[object Object](vue-ref-value). - Preserve reactive tracking: don't destructure reactive objects or watch plain unwrapped values (
vue-lost-reactivity). - No lifecycle hooks after
await: register hooks, watchers,computed, andinjectsynchronously during setup (vue-setup-after-await).
A ref without .value. Templates unwrap refs automatically, but inside <script> a ref is an object—always truthy in conditions and [object Object] in strings (vue-ref-value):
✕ Rejectedvue-ref-value
ts
import { computed, ref } from "vue";
export function useMenu() {
const open = ref(false);
const label = computed(() => (open ? "Close" : "Open"));
return { open, label };
}✓ Accepted0 diagnostics
ts
import { computed, ref } from "vue";
export function useMenu() {
const open = ref(false);
const label = computed(() => (open.value ? "Close" : "Open"));
return { open, label };
}Destructuring reactive state. const { id } = props copies the value id has now. It won't change when the prop does. Watching props.id has the same problem: watch gets a string, not something it can watch, and the callback never runs. vue-tsc refuses that one, where it runs; nothing type-checks the copy (vue-lost-reactivity):
✕ Rejectedvue-lost-reactivity
vue
<script setup lang="ts">
import { watch } from "vue";
const props = defineProps<{ id: string }>();
const { id } = props;
watch(props.id, (next) => console.log(`now showing ${next}`));
</script>
<template>
<p>{{ id }}</p>
</template>✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { watch } from "vue";
const props = defineProps<{ id: string }>();
watch(() => props.id, (next) => console.log(`now showing ${next}`));
</script>
<template>
<p>{{ props.id }}</p>
</template>The same goes for passing a value to a composable: useGreeting(props.name) hands it a string that never changes. Pass a getter, useGreeting(() => props.name), and read it with toValue inside:
✓ Accepted0 diagnostics
ts
import { computed, type MaybeRefOrGetter, toValue } from "vue";
export function useGreeting(name: MaybeRefOrGetter<string>) {
return computed(() => `Hello, ${toValue(name)}`);
}Hooks after await. Vue knows which component is being set up only until the first await in a function. A hook, watcher or computed registered after it isn't tied to the component: the hook never runs, the watcher is never stopped (vue-setup-after-await):
✕ Rejectedvue-setup-after-await
ts
import { shallowRef, watch, type ShallowRef } from "vue";
async function savedLanguage(): Promise<string> {
return "en";
}
export async function useLanguage(): Promise<ShallowRef<string>> {
const language = shallowRef(await savedLanguage());
watch(language, (value) => console.log(`language: ${value}`));
return language;
}✅ Accepted: register first, load afterwards
✓ Accepted0 diagnostics
ts
import { onMounted, shallowRef, watch } from "vue";
async function savedLanguage(): Promise<string> {
return "en";
}
export function useLanguage() {
const language = shallowRef("en");
watch(language, (value) => console.log(`language: ${value}`));
onMounted(async () => {
language.value = await savedLanguage();
});
return language;
}A top-level await in <script setup> is fine: Vue's compiler restores the component after it. Composables and other async functions don't get that.
Changing state while computing. A computed getter and the template must only read state. .sort() sorts the array in place, so this getter changes names every time it runs (vue-side-effect-in-render):
✕ Rejectedvue-side-effect-in-render
ts
import { computed, ref } from "vue";
export function useNames() {
const names = ref<string[]>([]);
const sorted = computed(() => names.value.sort((a, b) => a.localeCompare(b)));
return { names, sorted };
}✓ Accepted0 diagnostics
ts
import { computed, ref } from "vue";
export function useNames() {
const names = ref<string[]>([]);
const sorted = computed(() => [...names.value].sort((a, b) => a.localeCompare(b)));
return { names, sorted };
}Rules on this page: vue-ref-value, vue-lost-reactivity, vue-setup-after-await, vue-side-effect-in-render