Vue Component Generator
Build Vue 3 <script setup> components
with TypeScript-first props via defineProps<T>,
typed emits via defineEmits,
composables, and optional watch / provide-inject setup.
Copy the generated .vue file and drop it into your project.
script setup vs Options API: Quick Comparison
| Feature | Options API | script setup (Composition API) |
|---|---|---|
| Props | props: { title: String } | defineProps<{ title: string }>() |
| State | data() { return { count: 0 } } | const count = ref(0) |
| Computed | computed: { doubled() { ... } } | const doubled = computed(() => count.value * 2) |
| Methods | methods: { increment() { } } | function increment() { } |
| Lifecycle | mounted() { } | onMounted(() => { }) |
| Logic reuse | Mixins (fragile) | Composables (explicit, type-safe) |
Typed Props Pattern (Recommended)
{``} Building a Composable
A composable extracts reactive logic into a reusable function. Here's a minimal
useCounter pattern:
{`// composables/useCounter.ts
import { ref, computed } from 'vue';
export function useCounter(initial = 0) {
const count = ref(initial);
const doubled = computed(() => count.value * 2);
function increment() { count.value++; }
function reset() { count.value = initial; }
return { count, doubled, increment, reset };
}
// In component:
// const { count, increment } = useCounter(10);`}
Component Best Practices
- Always use
lang="ts" — TypeScript catches prop type mismatches at compile time - Prefer
defineProps<T>() over runtime props for full TypeScript inference - Default object/array props with factory functions:
items: () => [] - Name emits in kebab-case in templates (
@my-event) but camelCase in defineEmits - Use
<style scoped> — prevents style leakage between components - Extract logic over 30 lines into a composable in
composables/
Frequently Asked Questions
What is Vue 3 Composition API and script setup?▼
`<script setup>` is the recommended way to write Vue 3 components. It's syntactic sugar over the Composition API that removes boilerplate: no `export default`, no explicit `setup()` function, and all top-level declarations are automatically exposed to the template. Variables defined with `ref()` or `reactive()` are directly usable in templates. It pairs perfectly with TypeScript using `defineProps<Props>()` and `defineEmits<Emits>()`.
How do I define props with TypeScript in script setup?▼
Use `defineProps<Props>()` with a TypeScript interface. For default values, wrap with `withDefaults()`: `const props = withDefaults(defineProps<{ title: string; count?: number }>(), { count: 0 })`. The `Props` interface is type-checked at compile time by the Vue compiler. Avoid mixing runtime props declaration (`defineProps({ title: String })`) with the type-based approach in the same component.
What is the difference between ref() and reactive()?▼
`ref()` wraps a value in a reactive container — access it via `.value` in script, but the template auto-unwraps it. Use `ref` for primitives (strings, numbers, booleans) and for reactive object references you might reassign. `reactive()` makes an entire object deeply reactive — no `.value` needed. Drawback: you can't destructure a reactive() object without losing reactivity (use `toRefs()` to fix this). Stick with `ref` for most cases.
When should I use watch() vs watchEffect()?▼
`watch(source, callback)` runs only when the specific source changes, giving you both old and new values. It's lazy by default (doesn't run on mount). `watchEffect(callback)` tracks all reactive dependencies accessed inside it and runs immediately — similar to Svelte's $effect. Use `watch` when you need old values or want precise control over what triggers the watcher. Use `watchEffect` for effects that should run once immediately and re-run when any dependency changes.
What is a Vue composable and how should I structure one?▼
A composable is a function that uses the Composition API to encapsulate and reuse stateful logic. Name it with a `use` prefix (e.g., `useCounter`, `useFetch`). Return reactive state and methods as a plain object. Composables are called at the top level of `<script setup>` (not inside conditions or loops) so Vue can properly track their lifecycle. Libraries like VueUse provide hundreds of production-ready composables.
How do provide/inject work in Vue 3?▼
provide/inject is Vue's dependency injection — the ancestor-to-descendant alternative to prop drilling. A parent calls `provide('key', value)` to make a value available to all descendants. A descendant calls `const val = inject('key')` to receive it. For type safety, use InjectionKey: `const countKey: InjectionKey<Ref<number>> = Symbol('count')`. inject() accepts a default value as the second argument.
✓ 60,000+ developers use DevPlaybook
★★★★★ 4.8 / 5 on Gumroad ✓
MIT licensed · Instant download
✓
No login required
Related Products
on Gumroad 📘 $14 TypeScript Quick Reference Toolkit
5 practical reference files for TypeScript developers: cheatsheet, TS vs JS comparison, 20 utility types with real examples, tsconfig best practices template, and 15 common mistakes guide. Print it. Keep it on your desk.
Buy on Gumroad →
🔒 $29 DevPlaybook Interview Vault
210+ real technical interview questions with model answers, asked at Google, Meta, Amazon, Microsoft, Stripe, and Shopify. Covers Frontend (55), Backend (55), System Design (50), and DSA (50). Available in English and Traditional Chinese.
Buy on Gumroad →
🎯 $29 Developer Interview Vault
210 technical interview questions with model answers, key points, and follow-up questions. Covers Frontend, Backend, System Design, and DSA — bilingual English + Traditional Chinese (ZH-TW). PWYW from $29.
Buy on Gumroad →
You Might Also Need
SQL to TypeScript Types
Convert SQL CREATE TABLE statements to TypeScript interfaces and Zod schemas. Maps SQL column types to TypeScript with nullability.
Open free tool →
TypeScript Discriminated Union Builder
Build TypeScript discriminated unions visually. Add variants with fields and get type-safe switch statements, type guards, and exhaustive checking code.
Open free tool →
TypeScript Playground
Write TypeScript and instantly see compiled JavaScript output. Strip interfaces, types, generics, enums, and decorators. Share code via URL. No install needed.
Open free tool →
More Free Tools
SQL to TypeScript Types TypeScript Discriminated Union Builder TypeScript Playground TypeScript to JavaScript Converter TypeScript tsconfig.json Generator JS Prototype Chain Visualizer Neon DB Query Builder Code Tools JavaScript Formatter & Minifier JSON to TypeScript Interface Prettier Config Generator Zustand Store Builder GraphQL Client Configurator Axios Interceptor Builder Jest Mock Generator React Component Generator Turso Edge DB Explorer ESLint Config Generator Interview Question Generator Regex Visualizer Regex Tester Pro Regex Tester URL Parser & Builder Biome Config Generator Haproxy Config Validator CSS Container Query Builder Vitest Configuration Generator Regex Flags Explainer Jest Coverage Config Generator Bundle Size Estimator JavaScript Memory Leak Detector NPM Bundle Size Estimator Jest Testing Framework Guide Cypress Frontend Testing Guide AWS CDK Infrastructure as Code Guide Pulumi Modern IaC Guide JSON Schema to Zod Converter Drizzle Schema Generator Playwright Test Generator Pulumi Stack Config Generator Bundle Size Impact Calculator HTML5 Canvas API Playground Game Loop Calculator Bezier Curve Editor Particle System Config Generator TanStack Query Config Builder SWR Config Generator Jotai Atom Explorer OpenAI Function Calling Generator CSS Flexbox Visualizer Related Articles
Apr 2, 2026 · 13 min read TypeScript Best Practices 2025: Type Safety Patterns Every Developer Should Know
Master TypeScript type safety patterns in 2025. Branded types, discriminated unions, conditional types, template literals, and type guards with practical code examples.
Mar 28, 2026 · 14 min read SolidJS vs React 2026: Performance, DX, and When to Choose Each
Deep comparison of SolidJS and React in 2026: fine-grained reactivity vs virtual DOM, signals vs useState, performance benchmarks, SolidStart vs Next.js, and honest real-world tradeoffs.
Mar 28, 2026 · 10 min read TypeScript 5.5 New Features: Complete Guide for Developers 2026
Everything you need to know about TypeScript 5.5: inferred type predicates, isolated declarations, JSDoc @import tag, new Set methods, and performance improvements. With practical code examples.
Mar 28, 2026 · 10 min read Vitest vs Jest vs Mocha: Complete Testing Framework Comparison 2026
A comprehensive comparison of Vitest, Jest, and Mocha for JavaScript and TypeScript projects in 2026. Covers speed benchmarks, configuration, TypeScript support, mocking, code coverage, and migration strategies.
Mar 27, 2026 · 11 min read Pinia vs Vuex vs Zustand: State Management in 2026
Comparing Pinia, Vuex, and Zustand for state management in 2026. Bundle size, TypeScript support, DevTools, and which to pick for your project.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
DevPlaybook Pro 7 days free Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
Start Free Trial →
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle.
13 premium products — grab them all at once and save big.
$79 $241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →