TypeScript
- Using pnpm
- Global Configuration
- Page Components
- Page Props
- Form Helper
- Remembering State
- Restoring State
- Router Requests
- Scoped Flash Data
- Client-Side Visits
Inertia provides first-class TypeScript support. You may configure global types using declaration merging, and pass generics to hooks and router methods for type-safe props, forms, and state management.
Using pnpm
Due to pnpm's strict dependency isolation, @inertiajs/core is not accessible at node_modules/@inertiajs/core. Instead, it's nested inside .pnpm/, which prevents TypeScript module augmentation from resolving the module.
You may fix this by configuring pnpm to hoist the package. Add the following to your .npmrc file and run pnpm install.
1public-hoist-pattern[]=@inertiajs/core1public-hoist-pattern[]=@inertiajs/core
Alternatively, you may add @inertiajs/core as a direct dependency in your project.
1pnpm add @inertiajs/core1pnpm add @inertiajs/core
Global Configuration
You may configure Inertia's types globally by augmenting the InertiaConfig interface in the @inertiajs/core module. This is typically done in a global.d.ts file in your project's root or types directory.
1// global.d.ts2import '@inertiajs/core'34declare module "@inertiajs/core" {5 export interface InertiaConfig {6 sharedPageProps: {7 auth: { user: { id: number; name: string } | null };8 appName: string;9 };10 flashDataType: {11 toast?: { type: "success" | "error"; message: string };12 };13 errorValueType: string[];14 }15}1// global.d.ts2import '@inertiajs/core'34declare module "@inertiajs/core" {5 export interface InertiaConfig {6 sharedPageProps: {7 auth: { user: { id: number; name: string } | null };8 appName: string;9 };10 flashDataType: {11 toast?: { type: "success" | "error"; message: string };12 };13 errorValueType: string[];14 }15}
The import statement (or export {}) is required to make this file a module. Without it, declare module replaces the module definition instead of augmenting it. Your tsconfig.json also needs to include .d.ts files, so make sure a pattern like "resources/js/**/*.d.ts" is present in the include array.
Shared Page Props
The sharedPageProps option defines the type of data that is shared with every page in your application. With this configuration, page.props.auth and page.props.appName will be properly typed everywhere.
1sharedPageProps: {2 auth: { user: { id: number; name: string } | null }3 appName: string4}1sharedPageProps: {2 auth: { user: { id: number; name: string } | null }3 appName: string4}
Flash Data
The flashDataType option defines the type of flash data in your application.
1flashDataType: {2 toast?: { type: 'success' | 'error'; message: string }3}1flashDataType: {2 toast?: { type: 'success' | 'error'; message: string }3}
Error Values
By default, validation error values are typed as string. You may configure TypeScript to expect arrays instead for multiple errors per field.
1errorValueType: string[]1errorValueType: string[]
The next version of Laravel Wayfinder may automatically generate these types for you by analyzing your Laravel application. It generates TypeScript types for shared props, page props, form requests, and Eloquent models. This version is currently in beta.
Page Components
You may type the import.meta.glob result for better type safety when resolving page components.
1import { createInertiaApp } from "@inertiajs/vue3";2import type { DefineComponent } from "vue";34createInertiaApp({5 resolve: (name) => {6 const pages = import.meta.glob<DefineComponent>("./Pages/**/*.vue", {7 eager: true,8 });9 return pages[`./Pages/${name}.vue`];10 },11 // ...12});1import { createInertiaApp } from "@inertiajs/vue3";2import type { DefineComponent } from "vue";34createInertiaApp({5 resolve: (name) => {6 const pages = import.meta.glob<DefineComponent>("./Pages/**/*.vue", {7 eager: true,8 });9 return pages[`./Pages/${name}.vue`];10 },11 // ...12});
1import { createInertiaApp, type ResolvedComponent } from "@inertiajs/react";23createInertiaApp({4 resolve: (name) => {5 const pages = import.meta.glob<ResolvedComponent>("./Pages/**/*.tsx", {6 eager: true,7 });8 return pages[`./Pages/${name}.tsx`];9 },10 // ...11});1import { createInertiaApp, type ResolvedComponent } from "@inertiajs/react";23createInertiaApp({4 resolve: (name) => {5 const pages = import.meta.glob<ResolvedComponent>("./Pages/**/*.tsx", {6 eager: true,7 });8 return pages[`./Pages/${name}.tsx`];9 },10 // ...11});
1import { createInertiaApp, type ResolvedComponent } from "@inertiajs/svelte";23createInertiaApp({4 resolve: (name) => {5 const pages = import.meta.glob<ResolvedComponent>("./Pages/**/*.svelte", {6 eager: true,7 });8 return pages[`./Pages/${name}.svelte`];9 },10 // ...11});1import { createInertiaApp, type ResolvedComponent } from "@inertiajs/svelte";23createInertiaApp({4 resolve: (name) => {5 const pages = import.meta.glob<ResolvedComponent>("./Pages/**/*.svelte", {6 eager: true,7 });8 return pages[`./Pages/${name}.svelte`];9 },10 // ...11});
Page Props
You may type page-specific props by passing a generic to usePage(). These are merged with your global sharedPageProps, giving you autocomplete and type checking for both shared and page-specific data.
1<script setup lang="ts">2import { usePage } from "@inertiajs/vue3";34const page = usePage<{5 posts: { id: number; title: string }[];6}>();7</script>1<script setup lang="ts">2import { usePage } from "@inertiajs/vue3";34const page = usePage<{5 posts: { id: number; title: string }[];6}>();7</script>
1import { usePage } from "@inertiajs/react";23export default function Posts() {4 const page = usePage<{5 posts: { id: number; title: string }[];6 }>();78 return (9 <ul>10 {page.props.posts.map((post) => (11 <li key={post.id}>{post.title}</li>12 ))}13 </ul>14 );15}1import { usePage } from "@inertiajs/react";23export default function Posts() {4 const page = usePage<{5 posts: { id: number; title: string }[];6 }>();78 return (9 <ul>10 {page.props.posts.map((post) => (11 <li key={post.id}>{post.title}</li>12 ))}13 </ul>14 );15}
1<script lang="ts">2 import { usePage } from '@inertiajs/svelte'34 const page = usePage<{5 posts: { id: number; title: string }[]6 }>()7</script>1<script lang="ts">2 import { usePage } from '@inertiajs/svelte'34 const page = usePage<{5 posts: { id: number; title: string }[]6 }>()7</script>
Form Helper
The form helper accepts a generic type parameter for type-safe form data and error handling. This provides autocomplete for form fields and errors, and prevents typos in field names.
1<script setup lang="ts">2import { useForm } from "@inertiajs/vue3";34const form = useForm<{5 name: string;6 email: string;7 company: { name: string };8}>({9 name: "",10 email: "",11 company: { name: "" },12});13</script>1<script setup lang="ts">2import { useForm } from "@inertiajs/vue3";34const form = useForm<{5 name: string;6 email: string;7 company: { name: string };8}>({9 name: "",10 email: "",11 company: { name: "" },12});13</script>
1import { useForm } from "@inertiajs/react";23export default function CreateUser() {4 const form = useForm<{5 name: string;6 email: string;7 company: { name: string };8 }>({9 name: "",10 email: "",11 company: { name: "" },12 });1314 return null;15}1import { useForm } from "@inertiajs/react";23export default function CreateUser() {4 const form = useForm<{5 name: string;6 email: string;7 company: { name: string };8 }>({9 name: "",10 email: "",11 company: { name: "" },12 });1314 return null;15}
1<script lang="ts">2 import { useForm } from '@inertiajs/svelte'34 const form = useForm<{5 name: string6 email: string7 company: { name: string }8 }>({9 name: '',10 email: '',11 company: { name: '' },12 })13</script>1<script lang="ts">2 import { useForm } from '@inertiajs/svelte'34 const form = useForm<{5 name: string6 email: string7 company: { name: string }8 }>({9 name: '',10 email: '',11 company: { name: '' },12 })13</script>
Nested Data and Arrays
Form types fully support nested objects and arrays. You may access and update nested fields using dot notation, and error keys are automatically typed to match.
1import { useForm } from "@inertiajs/react";23const form = useForm<{4 user: { name: string; email: string };5 tags: { id: number; label: string }[];6}>({7 user: { name: "", email: "" },8 tags: [],9});1import { useForm } from "@inertiajs/react";23const form = useForm<{4 user: { name: string; email: string };5 tags: { id: number; label: string }[];6}>({7 user: { name: "", email: "" },8 tags: [],9});
Remembering State
The useRemember hook accepts a generic type parameter for type-safe local state persistence, providing autocomplete and ensuring values match the expected types.
1<script setup lang="ts">2import { useRemember } from "@inertiajs/vue3";34const filters = useRemember<{5 search: string;6 status: "active" | "inactive" | "all";7}>({8 search: "",9 status: "all",10});11</script>1<script setup lang="ts">2import { useRemember } from "@inertiajs/vue3";34const filters = useRemember<{5 search: string;6 status: "active" | "inactive" | "all";7}>({8 search: "",9 status: "all",10});11</script>
1import { useRemember } from "@inertiajs/react";23export default function Users() {4 const [filters, setFilters] = useRemember<{5 search: string;6 status: "active" | "inactive" | "all";7 }>({8 search: "",9 status: "all",10 });1112 return null;13}1import { useRemember } from "@inertiajs/react";23export default function Users() {4 const [filters, setFilters] = useRemember<{5 search: string;6 status: "active" | "inactive" | "all";7 }>({8 search: "",9 status: "all",10 });1112 return null;13}
1<script lang="ts">2 import { useRemember } from '@inertiajs/svelte'34 const filters = useRemember<{5 search: string6 status: 'active' | 'inactive' | 'all'7 }>({8 search: '',9 status: 'all',10 })11</script>1<script lang="ts">2 import { useRemember } from '@inertiajs/svelte'34 const filters = useRemember<{5 search: string6 status: 'active' | 'inactive' | 'all'7 }>({8 search: '',9 status: 'all',10 })11</script>
Restoring State
The router.restore() method accepts a generic for typing state restored from history.
1import { router } from "@inertiajs/react";23interface TableState {4 sortBy: string;5 sortDesc: boolean;6 page: number;7}89const restored = router.restore<TableState>("table-state");1011if (restored) {12 console.log(restored.sortBy);13}1import { router } from "@inertiajs/react";23interface TableState {4 sortBy: string;5 sortDesc: boolean;6 page: number;7}89const restored = router.restore<TableState>("table-state");1011if (restored) {12 console.log(restored.sortBy);13}
Router Requests
Router methods accept a generic for typing request data, providing type checking for the data being sent.
1import { router } from "@inertiajs/react";23interface CreateUserData {4 name: string;5 email: string;6}78router.post<CreateUserData>("/users", {9 name: "John",11});1import { router } from "@inertiajs/react";23interface CreateUserData {4 name: string;5 email: string;6}78router.post<CreateUserData>("/users", {9 name: "John",11});
Scoped Flash Data
The router.flash() method accepts a generic for typing page or section-specific flash data, separate from the global flashDataType configuration.
1import { router } from "@inertiajs/react";23router.flash<{ paymentError: string }>({ paymentError: "Card declined" });1import { router } from "@inertiajs/react";23router.flash<{ paymentError: string }>({ paymentError: "Card declined" });
Client-Side Visits
The router.push() and router.replace() methods accept a generic for typing client-side visit props.
1import { router } from "@inertiajs/react";23interface UserPageProps {4 user: { id: number; name: string };5}67router.push<UserPageProps>({8 component: "Users/Show",9 url: "/users/1",10 props: { user: { id: 1, name: "John" } },11});1213router.replace<UserPageProps>({14 props: (current) => ({15 ...current,16 user: { ...current.user, name: "Updated" },17 }),18});1import { router } from "@inertiajs/react";23interface UserPageProps {4 user: { id: number; name: string };5}67router.push<UserPageProps>({8 component: "Users/Show",9 url: "/users/1",10 props: { user: { id: 1, name: "John" } },11});1213router.replace<UserPageProps>({14 props: (current) => ({15 ...current,16 user: { ...current.user, name: "Updated" },17 }),18});