SvelteKit TypeScript
TypeScript integration in SvelteKit — $types for typed load functions, app.d.ts declarations (App.Locals, App.PageData, App.PageState, App.Platform, App.Error), and type-safe patterns across routing boundaries.
sveltekittypescripttype safetytypes
SvelteKit provides automatic type generation for load functions, ensuring data flows are type-safe across the server-client boundary.
Generated $types
When you create +page.svelte and +page.ts in the same route, SvelteKit generates a $types.d.ts file with typed interfaces:
import type { PageLoad } from './$types'
export const load: PageLoad = async ({ params, fetch }) => {
return { post: await fetchPost(params.slug) }
} Key types:
PageLoad/LayoutLoad— for universal load functionsPageServerLoad/LayoutServerLoad— for server-only load functionsPageData/LayoutData— returned data shapeActions/RequestHandler— for form actions and API routes
app.d.ts Declarations
The src/app.d.ts file is the central type declaration for SvelteKit’s application interface:
declare global {
namespace App {
interface Locals {
user: User | null
}
interface PageData {
title: string
}
interface PageState {
modal: boolean
}
interface Platform {
env: Env
context: ExecutionContext
}
interface Error {
code: number
message: string
}
}
}
export {} App.Locals— Type forevent.localsin hooks and load functionsApp.PageData— Type for$page.dataacross all routesApp.PageState— Type for custom page state (shallow routing)App.Platform— Type for platform-specific data (Cloudflare, etc.)App.Error— Type for error objects passed to+error.svelte
$app Types Module
The $app/types module provides utility types for working with SvelteKit’s stores and navigation:
import type { Page } from '$app/types' Best Practices
- Always type
App.Localsinapp.d.tsfor auth data - Use generated
$typesfor load function signatures - Type form action return values for safe form handling
- Use the
satisfieskeyword withPageLoadfor inline validation - Keep
app.d.tsminimal — only app-wide interfaces
See Also
- SvelteKit Data Loading — typed load functions
- SvelteKit Hooks and Server Runtime — typing locals
- SvelteKit Authentication — typed auth data
- Svelte 5 Runes — TypeScript with runes ($state, $derived)
