vue-template-ref
useElement("name") from @evobug_com/safe-ts-vue/element gives the element with ref="name", typed without null. That holds when the element is always rendered and is read after the component is mounted. This rule checks both:
- the template must always render the element: not under
v-if,v-for,<Match>or another component's slot; - its
.valueisn't read during setup, inonBeforeMountoronUnmounted, in acomputed(which can run during the first render), or after anawait.
It also reports a ref="name" that no element in the template has.
Why: Vue's own useTemplateRef is typed T | null, and ! is the usual answer: it hides the cases where the element really is missing. Guide: Vue.
✕ Rejectedvue-template-ref
vue
<script setup lang="ts">
import { useElement } from "@evobug_com/safe-ts-vue/element";
const props = defineProps<{ open: boolean }>();
const dialog = useElement<HTMLDialogElement>("dialog");
function show(): void {
dialog.value.showModal();
}
</script>
<template>
<dialog v-if="props.open" ref="dialog"></dialog>
<button @click="show()">Show</button>
</template>While open is false there is no dialog, and show() throws.
✅ Accepted: always rendered, read after mount
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { useElement } from "@evobug_com/safe-ts-vue/element";
const dialog = useElement<HTMLDialogElement>("dialog");
function show(): void {
dialog.value.showModal();
}
</script>
<template>
<dialog ref="dialog"></dialog>
<button @click="show()">Show</button>
</template>✅ Accepted: may be missing, so checked
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { useOptionalElement } from "@evobug_com/safe-ts-vue/element";
const props = defineProps<{ open: boolean }>();
const dialog = useOptionalElement<HTMLDialogElement>("dialog");
function show(): void {
dialog.value?.showModal();
}
</script>
<template>
<dialog v-if="props.open" ref="dialog"></dialog>
<button @click="show()">Show</button>
</template>Inside v-for, Vue sets the ref to a list: use useElements, which is empty until mounted. The quick fix switches to the helper that fits where the element is.
Functions declared in <script setup> and called later (from handlers, onMounted) may read the element. A function that reads it before its first await is reported where it is started too early:
- called during setup (
load()in<script setup>); - run by a watcher during setup:
watch(..., { immediate: true })orwatchEffect, also through a task'srun(const { run } = useTask(load)); - an
asyncfunction passed to a component as a prop (<PageLoader :load="load" />): the component may start it in its own setup, which runs before this component's template has rendered.
✕ Rejectedvue-template-ref
vue
<script setup lang="ts">
import { watch } from "vue";
import { useElement } from "@evobug_com/safe-ts-vue/element";
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { fetchProfile } from "./api";
const props = defineProps<{ id: string }>();
const heading = useElement<HTMLElement>("heading");
const { state, run } = useTask((signal) => load(signal));
function load(signal: AbortSignal): ReturnType<typeof fetchProfile> {
heading.value.focus();
return fetchProfile(signal);
}
watch(() => props.id, () => run().catch((reason: unknown) => console.error(reason)), { immediate: true });
</script>
<template>
<h1 ref="heading">{{ state.tag }}</h1>
</template>The first run starts during setup, and heading.value is still null. Read the element after the await (and check it: by then the component may be gone), start the first run in onMounted, or use useOptionalElement. If the template renders the element in a way the checker can't see, useElement logs an error when the component mounts without it.