Svelte Component Generator
Generate Svelte 5 component boilerplate with the new runes API —
$state,
$props,
$derived, and
$effect.
Define your props, events, and lifecycle hooks, then copy the ready-to-paste .svelte file.
Switch to Svelte 4 legacy mode for older projects.
Svelte 5 Runes vs Svelte 4: A Quick Comparison
| Concept | Svelte 4 | Svelte 5 (Runes) |
|---|---|---|
| Reactive state | let count = 0 | let count = $state(0) |
| Props | export let title: string | let { title } = $props() |
| Derived values | $: doubled = count * 2 | let doubled = $derived(count * 2) |
| Side effects | $: { sideEffect() } | $effect(() => { sideEffect() }) |
| Events | createEventDispatcher | Callback props (onEventName) |
| Lifecycle | onMount / onDestroy | $effect + cleanup return |
Svelte 5 Props with TypeScript
The runes API brings fully typed props without a separate interface declaration step. You define the type inline with destructuring:
{``} When to Use Svelte 4 Legacy Mode
Choose Svelte 4 mode when working on existing projects that haven't migrated to Svelte 5,
or when using libraries that depend on the legacy compiler behavior (like SvelteKit 1.x projects).
The createEventDispatcher pattern and
export let syntax remain fully supported
in Svelte 5 for backwards compatibility, but new projects should prefer runes.
Component File Checklist
- Use PascalCase for component filenames (
MyButton.svelte) - Prefer
lang="ts" for TypeScript benefits even in Svelte 4 - Keep
<style> scoped (the default) — use :global() sparingly - For Svelte 5, avoid mixing
$state with non-reactive objects — deep reactivity needs explicit $state() - Use
$inspect() during development for reactive value debugging
Frequently Asked Questions
What is the difference between Svelte 5 runes and Svelte 4 reactive syntax?▼
Svelte 5 introduced runes — a set of compiler hints prefixed with $ that make reactivity explicit. Instead of `let count = 0` (Svelte 4, where any top-level let was reactive), you now write `let count = $state(0)`. Similarly, $props() replaces `export let`, $derived() replaces `$:` reactive declarations, and $effect() replaces `onMount` + reactive blocks. Runes work inside any .svelte.ts file, not just components.
How do I pass props in Svelte 5?▼
In Svelte 5, props are declared with `$props()`: `let { title, count = 0 }: { title: string; count?: number } = $props()`. You can destructure with defaults inline. This replaces `export let title: string` from Svelte 4. For spread props, use `let { ...rest } = $props()`. The TypeScript interface provides type safety without a separate `defineProps` call.
How do events work in Svelte 5 vs Svelte 4?▼
In Svelte 4, components emitted events via `createEventDispatcher`. Svelte 5 replaces this with callback props — you accept an `onEventName` function prop and call it directly, which is more TypeScript-friendly and avoids event bubbling surprises. Example: `let { onSubmit }: { onSubmit: (data: FormData) => void } = $props()`. In templates, use `onclick` (lowercase) instead of `on:click`.
What does $effect() do in Svelte 5?▼
$effect() runs a side effect whenever its reactive dependencies change — similar to React's useEffect but without a dependency array. It tracks dependencies automatically. Return a cleanup function to run before the effect re-runs or the component unmounts. Use $effect.pre() to run before DOM updates. For one-time setup, $effect() with no reactive reads acts like onMount.
When should I use $derived() vs computed values?▼
$derived() creates a reactive value that recomputes whenever its dependencies change. Use it for values computed from state: `let total = $derived(items.reduce((s, i) => s + i.price, 0))`. Unlike $effect(), $derived() is pure (no side effects) and lazy (only recomputes when read). For complex multi-statement derivations, use `$derived.by(() => { ... return value; })`.
What is the Svelte component file structure?▼
A .svelte file has three optional sections: `<script>` (or `<script lang='ts'>`) for logic, the template (HTML with Svelte directives, no wrapper tag needed), and `<style>` for scoped CSS. In Svelte 5, use `<script lang='ts'>` at the top. Svelte automatically scopes styles to the component, so `.button` in a component won't affect other `.button` elements globally.
✓ 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 · 11 min read Svelte 5 Runes: Complete Migration Guide 2026
Master Svelte 5 runes: $state, $derived, $effect, $props, and $bindable. Learn how to migrate from Svelte 4 stores and reactive declarations with practical examples.
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.
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 →