Setup
SAFE-TS for Vue uses the same Rust checker and build transform as every SAFE-TS project (Adopting SAFE-TS covers tsconfig.json and oxlint), plus @evobug_com/safe-ts-vue for Vue 3.5+ component helpers.
In short
- Install the Vue helpers: add
@evobug_com/safe-ts-vuealongside@evobug_com/safe-ts-std,@evobug_com/safe-ts-transform, and the oxlint plugin. - Plugin order matters: register
safeTs(...)beforevue()invite.config.tsso components are checked and<Match>is rewritten before Vue compiles templates. - Full coverage: Vite dev/build,
safe-ts check,safe-ts lsp, and oxlint all inspect.vuetemplates,<script setup>, and composables.
sh
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-vueRegister the SAFE-TS plugin before @vitejs/plugin-vue so it checks each SFC and rewrites q(...), match(...), parseJson<T>, and <Match> prior to Vue's template compiler:
ts
// vite.config.ts
import { safeTs } from "@evobug_com/safe-ts-transform";
import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";
export default defineConfig({ plugins: [safeTs({ check: "error" }), vue()] });See examples/vue/ for a complete working app, or try the live comparison in the Vue demo.
Where the checks run:
.vue files | Composables (.ts files importing vue) | |
|---|---|---|
| Vite dev server and build | ✅ | ✅ |
safe-ts check src/main.ts | ✅ it follows .vue imports | ✅ |
Editor (safe-ts lsp) | ✅ | ✅ |
| Editor and CI (oxlint plugin) | ✅ script blocks, with the template | ✅ |
Codemods (safe-ts migrate) | ✅ scripts and template | ✅ |
| Type checking | vue-tsc | tsc |
In the editor, use the SAFE-TS language server: it checks .vue files whole, template included, and points at their lines. In VS Code, the extension starts it; it works without the Vue extension too (Editor).
oxlint and .vue files
oxlint runs JS plugins on .vue files (every version tried, 1.80 to 1.94), but hands a plugin only one script block's text at a time, under the component's file name, and never the template. The SAFE-TS plugin needs the whole component, so it finds that block in the file on disk and checks the whole file:
- Problems in a script block are reported on that block, at their line.
- Problems in the template are reported once, at the top of
<script setup>(or of the only<script>), with their place in the message:template line 12:7: v-for without :key. - In the editor, an unsaved component with one script block is checked with your edits. With two script blocks (
<script>and<script setup>), the edited block reports "save it to check this script block" until you save. - A component without a script block (an icon, a layout) is never handed to plugins.
SAFE-TS check, the language server and the Vite plugin check it.
Up to 0.3.0 the plugin read the script block as the whole component: it reported nothing, and a non-ASCII character in the script (é, ·) aborted oxlint. Projects that turned the safe-ts/* rules off for **/*.vue in an oxlint overrides entry can drop it.
safe-ts emit doesn't write .vue files: tsc can't read them, so type-check components with vue-tsc.
The codemods rewrite components with the .ts files they import, in one run: safe-ts migrate null-to-undefined src/ changes a store's string | null and the === null in the templates reading it together. Give them the whole src/, then run vue-tsc. What they leave in components, and why, is in Vue components.
By default (the oxlint preset's levels, which safe-ts check, the language server and the Vite plugin use too), vue-render-cost, vue-deep-watch and vue-prefer-shallow-ref are warnings; the other Vue rules are errors. package.json can change a rule's level for the package or some directories (rule levels). To keep one element as it is, put <!-- safe-ts-ignore <rule>: <reason> --> before it in the template (ignoring one finding).
Legacy components
A component that isn't migrated yet gets the legacy marker, like a .ts file (Adoption). Put it on the first line inside either <script> block, or as an HTML comment at the top of the file, and list the component under "legacyFiles" in package.json ("legacyFiles": ["src/components/Old.vue"]; an unlisted marker is reported by unlisted-marker):
vue
<!-- @safe-ts legacy -->
<template>
<p>{{ title }}</p>
</template>
<script setup lang="ts">
const title = "Not migrated yet";
</script>Either form marks the whole component, its template included. A marker anywhere else (further down a script, inside the template) marks nothing, and is reported by vue-sfc.
The .vue shim
A declare module "*.vue" file tells a type checker what a .vue import is when it doesn't read the component itself. It has two jobs, and one file can't always do both:
For
vue-tsc, which reads the components itself, and oxlint's type-aware rules, which see a.vueimport through the shim, it only has to be a valid component type:ts// src/shims-vue.d.ts declare module "*.vue" { import type { DefineComponent } from "vue"; const component: DefineComponent; export default component; }safe-ts checkcounts a wildcard module like this one among the ambient declarations and doesn't report it: it is what the bundler makes of the file (no-unchecked-declare). A shim with no body (declare module "*.vue";) is reported: every import from it isany.The form
export default InstanceType<Component>is not one. In that position TypeScript reads a value, finds none namedInstanceType(TS2693, hidden byskipLibCheckin a.d.ts), and types every.vueimport as an error type. Type-aware lint then reportstypescript/no-unsafe-argumentwherever a component is passed on.For a check that reads no
.vuefile (plaintsc, or a TypeScript 7 run over the.tsfiles), every import is what the shim says. When the.tscode calls what a component exposes (dialog.value?.close()on a template ref),DefineComponentwith no type arguments refuses it. Give that check a looser shim of its own, in a file only its tsconfig includes, and keep the one above forvue-tscand oxlint:ts// types/shims-vue.ts7.d.ts: only tsconfig.ts7.json includes it declare module "*.vue" { import type { DefineComponent } from "vue"; // Any props, and what the .ts code calls on a component instance. const component: DefineComponent<Record<string, unknown>, { close: () => void }>; export default component; }
Scripts must be TypeScript
A script without lang="ts" has no types to check, and an Options API component keeps its state, methods and hooks in an object the checker can't follow. Both are reported by vue-sfc:
✕ Rejectedvue-sfc
vue
<script setup>
const greeting = "Hello";
</script>
<template>
<p>{{ greeting }}</p>
</template>How a template is read
Each part of a template becomes the code Vue runs. Your expressions are copied as written:
| Template | Read as |
|---|---|
{{ e }}, :value="e", v-show="e" | e, run on every render |
@click="save" | save($event), in a function Vue calls later |
@click="save(item)", @click="count++" | that code, in a function Vue calls later |
v-model="x" | x = $event, in a function Vue calls later |
v-if / v-else-if / v-else | if / else if / else |
v-for="(item, i) in items" | a for...of loop |
#row="{ item }" | a function called right away: a slot renders with its parent |
<Match :value="state"> | checked against the variants of state's type |
So the rules you know apply in templates as they do anywhere else:
✕ Rejectedunhandled-failureno-null
vue
<script setup lang="ts">
const props = defineProps<{ settings: string }>();
</script>
<template>
<pre>{{ JSON.parse(props.settings) }}</pre>
<input :value="null" />
</template>Rules on this page: vue-sfc