Skip to content

Values that may be missing ​

Empty ref() calls, optional props without defaults, unrequired defineModel(), and inject() without a fallback all produce undefined. While TypeScript guards them in <script setup>, unguarded template reads like {{ user.name }} compile without vue-tsc and crash on first render.

In short

  • Templates are checked statically: vue-maybe-undefined tracks v-if / v-else-if / v-else, &&, ?:, ?., and destructured prop defaults.
  • No null template refs: use useElement("name") for always-mounted elements and useOptionalElement("name") under v-if (details).

SAFE-TS reads templates as executable code and requires a guard before dereferencing potentially undefined values:

vue
<script setup lang="ts">
const { user } = defineProps<{ user?: { name: string } }>();
</script>

<template>
  <h1>{{ user.name }}</h1>
</template>
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
const { user, title = "Profile" } = defineProps<{ user?: { name: string }; title?: string }>();
</script>

<template>
  <h1>{{ title.toUpperCase() }}</h1>
  <p v-if="user">{{ user.name }}</p>
  <p v-else>Nobody is signed in.</p>
</template>

A default (title = "Profile") means the prop is never undefined. The quick fix writes ?. (user?.name), which shows nothing while the value is missing; wrapping the part in v-if usually says more.

For data that is loaded, don't let it start as undefined at all: useTask and <Match> give "not loaded yet", "loading" and "failed" a slot of their own (see Async work).

Template refs: useElement ​

useTemplateRef("input") is typed HTMLInputElement | null, because Vue fills it in when it mounts the element. Most elements are always rendered, so the null is noise, and ! hides the one case where it is real. useElement from @evobug_com/safe-ts-vue/element is typed without null, and the checker proves it (vue-template-ref):

  • the template must always render the element: not under v-if, v-for, <Match> or another component's slot;
  • it must be read after mount: in onMounted / onUpdated, in an event handler or in a function those call, and before any await (the component may be gone by then).
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { onMounted } from "vue";
import { useElement } from "@evobug_com/safe-ts-vue/element";

const input = useElement<HTMLInputElement>("input");
onMounted(() => input.value.focus());
</script>

<template>
  <input ref="input" />
  <button @click="input.select()">Select all</button>
</template>
✕ Rejectedvue-template-ref
vue
<script setup lang="ts">
import { useElement } from "@evobug_com/safe-ts-vue/element";

const props = defineProps<{ editing: boolean }>();
const input = useElement<HTMLInputElement>("input");
input.value.focus();
</script>

<template>
  <input v-if="props.editing" ref="input" />
</template>

Two problems: the element is under v-if, and setup runs before anything is rendered. For an element that may be missing, use useOptionalElement (undefined while it isn't there, so TypeScript makes you check); inside v-for, useElements (the list of elements). The quick fix switches to the right one.

Rules on this page: vue-maybe-undefined, vue-template-ref

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra