A Vue template is executable code: JSON.parse inside it can throw, @click="save" silently drops a returned Result, and v-model="props.user.name" mutates the parent's state. SAFE-TS checks script and template together—the way Vue actually runs them.
Interactive Live Comparison
Pick someone above to start a request.
Pick someone above to start a request.
Both components run in your browser against a fake server: Ada takes 2 s, Grace takes 0.4 s, and Linus fails. Try picking Linus first, or Ada then Grace before Ada finishes. Read the full code & diagnostics behind this demo.
Vue Architecture
Every SAFE-TS rule runs in the template just like in <script setup>: unhandled throws, dropped Results, null, and reading optional props before guarding with v-if.
Props belong to the parent. Read them; emit an event or use defineModel() to update them. State in <script setup> stays per-instance, never shared in plain <script>.
Replace disagreeing loading, error, and data refs with one AsyncState. useTask aborts superseded runs automatically, and <Match> checks every slot exhaustively.
Vue invokes event handlers with nothing of yours around them and discards return values. Errors must be handled inside the handler where the UI can report them.
Side-by-Side · Checked with vue-tsc
Every pair below is a real Vue SFC verified by cargo test and vue-tsc with strict templates.
Vue discards handler return values, so the error publish returns disappears silently.
<script setup lang="ts">
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
type PublishError = Enum<{ Empty: {}; TooLong: { max: number } }>;
const PublishError = constructors<PublishError>();
const props = defineProps<{ draft: string }>();
function publish(): Result<void, PublishError> {
if (props.draft === "") return err(PublishError.Empty());
if (props.draft.length > 280) return err(PublishError.TooLong({ max: 280 }));
return undefined;
}
</script>
<template>
<button @click="publish">Publish</button>
</template>Model component state as one Enum (Editing, Rejected, Published) and render every state exhaustively with <Match>.
<script setup lang="ts">
import { shallowRef } from "vue";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
import { Match } from "@evobug_com/safe-ts-vue/match";
type PublishError = Enum<{ Empty: {}; TooLong: { max: number } }>;
const PublishError = constructors<PublishError>();
type Publishing = Enum<{ Editing: {}; Rejected: { error: PublishError }; Published: {} }>;
const Publishing = constructors<Publishing>();
const props = defineProps<{ draft: string }>();
const emit = defineEmits<{ publish: [text: string] }>();
const state = shallowRef<Publishing>(Publishing.Editing());
function publish(): Result<void, PublishError> {
if (props.draft === "") return err(PublishError.Empty());
if (props.draft.length > 280) return err(PublishError.TooLong({ max: 280 }));
emit("publish", props.draft);
return undefined;
}
function onPublish(): void {
const published = publish();
state.value = isErr(published) ? Publishing.Rejected({ error: published.error }) : Publishing.Published();
}
</script>
<template>
<Match :value="state">
<template #Editing>
<button @click="onPublish">Publish</button>
</template>
<template #Rejected="{ error }">
<button @click="onPublish">Publish</button>
<Match :value="error">
<template #Empty>Write something first.</template>
<template #TooLong="{ max }">Keep it under {{ max }} characters.</template>
</Match>
</template>
<template #Published>Published.</template>
</Match>
</template>More in Errors in handlers.
Vue only makes the top-level props object read-only—nested objects mutate the parent's data behind its back.
<script setup lang="ts">
type User = { name: string; email: string };
const props = defineProps<{ user: User }>();
function clearEmail(): void {
props.user.email = "";
}
</script>
<template>
<input v-model="props.user.name" />
<button @click="clearEmail">Clear email</button>
</template>Use defineModel() and defineEmits() so the component requests changes and the parent owns the state.
<script setup lang="ts">
const name = defineModel<string>("name", { required: true });
const emit = defineEmits<{ clearEmail: [] }>();
</script>
<template>
<input v-model="name" />
<button @click="emit('clearEmail')">Clear email</button>
</template>More in State and props.
Three refs that drift out of sync, plus an uncancelled async watcher where a slow older response overwrites a newer one.
<script setup lang="ts">
import { ref, watch } from "vue";
import { isErr } from "@evobug_com/safe-ts-std/result";
import { fetchUser, type User } from "./api";
const props = defineProps<{ id: string }>();
const loading = ref(false);
const failed = ref("");
const user = ref<User>();
watch(() => props.id, async (id) => {
loading.value = true;
const result = await fetchUser(id);
loading.value = false;
if (isErr(result)) failed.value = result.error.message;
else user.value = result;
}, { immediate: true });
</script>
<template>
<p v-if="loading">Loading…</p>
<p v-else-if="failed !== ''">{{ failed }}</p>
<p v-else-if="user">{{ user.name }}</p>
</template>One AsyncState value and one slot per variant. Each run(id) aborts the previous request, and <Match> compiles into a plain v-if chain.
<script setup lang="ts">
import { watch } from "vue";
import { Match } from "@evobug_com/safe-ts-vue/match";
import { useTask } from "@evobug_com/safe-ts-vue/task";
import { fetchUser } from "./api";
const props = defineProps<{ id: string }>();
const { state, run } = useTask((signal, id: string) => fetchUser(id, signal));
watch(() => props.id, (id) => run(id), { immediate: true });
</script>
<template>
<Match :value="state">
<template #Loading>Loading…</template>
<template #Ready="{ value }">{{ value.name }}</template>
<template #Failed="{ error }">Couldn't load the user: {{ error.message }}</template>
<template #_></template>
</Match>
</template>More in Async state, including template ref timing.
user is optional. Without vue-tsc, templates aren't type-checked—so this compiles and crashes on first render.
<script setup lang="ts">
const { user } = defineProps<{ user?: { name: string } }>();
</script>
<template>
<h1>{{ user.name }}</h1>
</template>Guard the optional value in the template before reading its properties.
<script setup lang="ts">
const { user } = defineProps<{ user?: { name: string } }>();
</script>
<template>
<h1 v-if="user">{{ user.name }}</h1>
<p v-else>Nobody is signed in.</p>
</template>More in Values that may be missing.
v-html injects unsanitized markup, and a bound :href lets javascript: URLs execute on click.
<script setup lang="ts">
type Comment = { readonly author: string; readonly website: string; readonly body: string };
const props = defineProps<{ comment: Comment }>();
</script>
<template>
<a :href="props.comment.website">{{ props.comment.author }}</a>
<div v-html="props.comment.body"></div>
</template>Validate dynamic URLs with safeHref and let Vue escape plain text automatically.
<script setup lang="ts">
import { safeHref } from "@evobug_com/safe-ts-vue/url";
type Comment = { readonly author: string; readonly website: string; readonly body: string };
const props = defineProps<{ comment: Comment }>();
</script>
<template>
<a :href="safeHref(props.comment.website)">{{ props.comment.author }}</a>
<p>{{ props.comment.body }}</p>
</template>More in Security.
Migration Cheatsheet
| Idiom | Reported by | SAFE-TS pattern |
|---|---|---|
ref<HTMLElement | null>(null) for a template ref | no-null | useElement("name") for always-rendered elements, useOptionalElement under v-if (Missing values) |
ref<User>() filled in onMounted and shown | vue-ref-without-value | useTask + <Match> (Async state); local UI selections can still start empty (ref<Row | undefined>(undefined)) |
watch(settings, save, { deep: true }) | vue-deep-watch when reading a few fields | Watch only the fields used; saving the whole object is accepted as-is |
v-for over an object or Object.entries(x) | vue-v-for-key for index keys | Key by entry name: v-for="(count, name) in counts" :key="name" |
defineProps<{ size?: Size }>() with exactOptionalPropertyTypes | vue-tsc when passing undefined | Provide a destructured default (const { size = "md" } = defineProps<...>()), or type size?: Size | undefined |
Tooling & Setup
| Surface | .vue SFCs | Composables (.ts importing vue) |
|---|---|---|
| Vite dev server & build | ✅ | ✅ |
safe-ts check src/main.ts | ✅ follows .vue imports | ✅ |
Editor (safe-ts lsp) | ✅ | ✅ |
| Editor & CI (oxlint plugin) | ✅ script + template | ✅ |
| Type checking | vue-tsc | tsc |
Server-side rendering adds two more rules (SSR), and vue-router ships its own feature pack (vue-router).
npm install -D @evobug_com/safe-ts-transform @evobug_com/oxlint-plugin-safe-ts oxlint
npm install @evobug_com/safe-ts-std @evobug_com/safe-ts-vueimport { safeTs } from "@evobug_com/safe-ts-transform";
import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";
// safeTs before vue: checks each component and rewrites <Match> before Vue compiles it.
export default defineConfig({ plugins: [safeTs({ check: "error" }), vue()] });git clone https://git.evobug.com/Evobug/Safe-Ts.git
cd Safe-Ts
npm install
npm run build
npm run dev -w examples/vueFull tsconfig, oxlint, and prelude setup is in Vue Setup.