Skip to main content
Svelte has excellent built-in TypeScript support, allowing you to catch errors early and improve code quality with static type checking.

Basic Setup

Using <script lang="ts">

To use TypeScript in your Svelte components, add the lang="ts" attribute to your script tags:
Only type-only features are supported out of the box. Features that require TypeScript to output code (like enums or decorator metadata) need a preprocessor.

Type-Only Features Supported

Svelte’s built-in TypeScript support includes:
  • Type annotations
  • Interface declarations
  • Type assertions
  • Generic types
  • Type imports/exports

Features Requiring a Preprocessor

The following TypeScript features require a preprocessor setup:
  • Enums
  • Parameter properties (private, protected, public modifiers in constructors)
  • Features not yet in ECMAScript Stage 4

Preprocessor Configuration

Using Vite or SvelteKit

For full TypeScript support, configure vitePreprocess in your svelte.config.js:
svelte.config.js

Other Build Tools

For Rollup or Webpack:
  1. Install dependencies:
  2. Configure the preprocessor in your bundler plugin configuration
For new projects, we strongly recommend using SvelteKit or Vite instead of Rollup/Webpack for better TypeScript integration.

TypeScript Configuration

Required tsconfig.json Settings

Ensure your tsconfig.json includes these critical settings:
tsconfig.json
  • target: Use at least ES2015 so classes aren’t transpiled to functions
  • verbatimModuleSyntax: Keeps import/export statements as-is
  • isolatedModules: Ensures each file can be compiled independently (required for Vite)

Typing Components

Typing $props

Define props using TypeScript interfaces:

Generic Components

Create components with generic type relationships using the generics attribute:
The generics attribute accepts the same syntax as TypeScript generic parameters:
  • Multiple generics: generics="T, U"
  • Constraints: generics="T extends string"
  • Defaults: generics="T = string"

Typing Wrapper Components

For components that wrap native elements, use types from svelte/elements:
For elements without dedicated types:

Typing State and Derived Values

Typing $state

Type state variables like regular TypeScript variables:
Without an initial value, the type includes undefined:
Use type assertions when you know the value will be defined:

Typing $derived

Derived values infer their type from the expression:

Component Type Utilities

The Component Type

Use the Component type to constrain dynamic components:

Extracting Component Props

Use ComponentProps to get a component’s prop types:

Component Constructor and Instance Types

Extending DOM Types

For custom or experimental attributes, augment the svelte/elements module:
additional-svelte-typings.d.ts
Make sure the .d.ts file is included in your tsconfig.json (e.g., in the src directory with "include": ["src/**/*"]).

IDE Support

VS Code Extension

Install the Svelte for VS Code extension for:
  • TypeScript error checking in your editor
  • Autocompletion for Svelte syntax
  • Type information on hover
  • Automatic imports

Command Line Checking

Use svelte-check for CI/CD integration:

Best Practices

Avoid using features that require runtime type information (like decorators or reflection). Svelte’s TypeScript support is compile-time only.