Vue 3 Best Practices
Quick Reference
| Topic |
When to Use |
Reference |
| TypeScript |
Props extraction, generic components, useTemplateRef, JSDoc, reactive props destructure |
typescript.md |
| Volar |
IDE config, strictTemplates, CSS modules, directive comments, Volar 3.0 migration |
volar.md |
| Components |
defineModel, deep watch, onWatcherCleanup, useId, deferred teleport |
components.md |
| Tooling |
moduleResolution, HMR SSR, duplicate plugin detection |
tooling.md |
| Testing |
Pinia store mocking, setup stores, Vue Router typed params |
testing.md |
Essential Patterns
Extract Component Props
import type { ComponentProps } from 'vue-component-type-helpers'
import MyButton from './MyButton.vue'
type Props = ComponentProps<typeof MyButton>
Reactive Props Destructure (Vue 3.5+)
<script setup lang="ts">
// Destructured props are reactive - preferred in Vue 3.5+
const { name, count = 0 } = defineProps<{ name: string; count?: number }>()
</script>
useTemplateRef (Vue 3.5+)
<script setup lang="ts">
import { useTemplateRef, onMounted } from 'vue'
const inputRef = useTemplateRef('input') // Auto-typed
onMounted(() => inputRef.value?.focus())
</script>
<template><input ref="input" /></template>
onWatcherCleanup (Vue 3.5+)
import { watch, onWatcherCleanup } from 'vue'
watch(query, async (q) => {
const controller = new AbortController()
onWatcherCleanup(() => controller.abort())
await fetch(`/api?q=${q}`, { signal: controller.signal })
})
defineModel with Required
// Returns Ref<Item> instead of Ref<Item | undefined>
const model = defineModel<Item>({ required: true })
Deep Watch with Numeric Depth
// Vue 3.5+ - watch array mutations without full traversal
watch(items, handler, { deep: 1 })
Pinia Store Test Setup
import { createTestingPinia } from '@pinia/testing'
import { vi } from 'vitest'
mount(Component, {
global: {
plugins: [createTestingPinia({ createSpy: vi.fn })]
}
})
Common Mistakes
- Using
InstanceType<typeof Component>['$props'] - Use ComponentProps instead
- Missing
createSpy in createTestingPinia - Required in @pinia/testing 1.0+
- Using
withDefaults with union types - Use Reactive Props Destructure
strictTemplates in wrong tsconfig - Add to tsconfig.app.json, not root
- ts_ls with Volar 3.0 - Use vtsls instead (Neovim)
deep: true on large structures - Use numeric depth for performance
- Watching destructured props directly - Wrap in getter:
watch(() => count, ...)
- Random IDs in SSR - Use
useId() for hydration-safe IDs
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: vue-best-practices-283description: Vue 3 and Vue.js best practices for TypeScript, vue-tsc, Volar, and component patterns. Use when writing, reviewing, or refactoring Vue 3 components with TypeScript, configuring Volar/vueCompilerOptions, extracting component types, working with defineModel/withDefaults, setting up Pinia store tests, or debugging Vue tooling issues. Triggers on Vue components, props extraction, wrapper components, template type checking, strictTemplates, vueCompilerOptions, Volar 3, CSS modules, fallthrough attributes, defineModel, withDefaults, deep watch, vue-router typed params, Pinia mocking, HMR SSR, moduleResolution bundler, useTemplateRef, onWatcherCleanup, useId, generic components, reactive props destructure. Use when this capability is needed.4---56# Vue 3 Best Practices78## Quick Reference910| Topic | When to Use | Reference |11|-------|-------------|-----------|12| **TypeScript** | Props extraction, generic components, useTemplateRef, JSDoc, reactive props destructure | [typescript.md](references/typescript.md) |13| **Volar** | IDE config, strictTemplates, CSS modules, directive comments, Volar 3.0 migration | [volar.md](references/volar.md) |14| **Components** | defineModel, deep watch, onWatcherCleanup, useId, deferred teleport | [components.md](references/components.md) |15| **Tooling** | moduleResolution, HMR SSR, duplicate plugin detection | [tooling.md](references/tooling.md) |16| **Testing** | Pinia store mocking, setup stores, Vue Router typed params | [testing.md](references/testing.md) |1718## Essential Patterns1920### Extract Component Props2122```typescript23import type { ComponentProps } from 'vue-component-type-helpers'24import MyButton from './MyButton.vue'2526type Props = ComponentProps<typeof MyButton>27```2829### Reactive Props Destructure (Vue 3.5+)3031```vue32<script setup lang="ts">33// Destructured props are reactive - preferred in Vue 3.5+34const { name, count = 0 } = defineProps<{ name: string; count?: number }>()35</script>36```3738### useTemplateRef (Vue 3.5+)3940```vue41<script setup lang="ts">42import { useTemplateRef, onMounted } from 'vue'4344const inputRef = useTemplateRef('input') // Auto-typed45onMounted(() => inputRef.value?.focus())46</script>47<template><input ref="input" /></template>48```4950### onWatcherCleanup (Vue 3.5+)5152```typescript53import { watch, onWatcherCleanup } from 'vue'5455watch(query, async (q) => {56 const controller = new AbortController()57 onWatcherCleanup(() => controller.abort())58 await fetch(`/api?q=${q}`, { signal: controller.signal })59})60```6162### defineModel with Required6364```typescript65// Returns Ref<Item> instead of Ref<Item | undefined>66const model = defineModel<Item>({ required: true })67```6869### Deep Watch with Numeric Depth7071```typescript72// Vue 3.5+ - watch array mutations without full traversal73watch(items, handler, { deep: 1 })74```7576### Pinia Store Test Setup7778```typescript79import { createTestingPinia } from '@pinia/testing'80import { vi } from 'vitest'8182mount(Component, {83 global: {84 plugins: [createTestingPinia({ createSpy: vi.fn })]85 }86})87```8889## Common Mistakes90911. **Using `InstanceType<typeof Component>['$props']`** - Use `ComponentProps` instead922. **Missing `createSpy` in createTestingPinia** - Required in @pinia/testing 1.0+933. **Using `withDefaults` with union types** - Use Reactive Props Destructure944. **`strictTemplates` in wrong tsconfig** - Add to `tsconfig.app.json`, not root955. **ts_ls with Volar 3.0** - Use vtsls instead (Neovim)966. **`deep: true` on large structures** - Use numeric depth for performance977. **Watching destructured props directly** - Wrap in getter: `watch(() => count, ...)`988. **Random IDs in SSR** - Use `useId()` for hydration-safe IDs99100---101> Converted and distributed by [TomeVault](https://tomevault.io/claim/ejirocodes) — claim your Tome and manage your conversions.102<!-- tomevault:4.0:skill_md:2026-04-11 -->