Skip to content

TypeScript

Advanced

TypeScript

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/core
1public-hoist-pattern[]=@inertiajs/core

Alternatively, you may add @inertiajs/core as a direct dependency in your project.

1pnpm add @inertiajs/core
1pnpm 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.ts
2import '@inertiajs/core'
3 
4declare 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 layoutProps: {
15 title: string;
16 showSidebar: boolean;
17 };
18 namedLayoutProps: {
19 app: { title: string; theme: "light" | "dark" };
20 content: { padding: string; maxWidth: string };
21 };
22 }
23}
1// global.d.ts
2import '@inertiajs/core'
3 
4declare 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 layoutProps: {
15 title: string;
16 showSidebar: boolean;
17 };
18 namedLayoutProps: {
19 app: { title: string; theme: "light" | "dark" };
20 content: { padding: string; maxWidth: string };
21 };
22 }
23}

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: string
4}
1sharedPageProps: {
2 auth: { user: { id: number; name: string } | null }
3 appName: string
4}

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[]

Layout Props

The layoutProps option types the data accepted by setLayoutProps(). The namedLayoutProps option types the data accepted by setLayoutProps('name', props), keyed by layout name.

1layoutProps: {
2 title: string
3 showSidebar: boolean
4}
5namedLayoutProps: {
6 app: { title: string; theme: 'light' | 'dark' }
7 content: { padding: string; maxWidth: string }
8}
1layoutProps: {
2 title: string
3 showSidebar: boolean
4}
5namedLayoutProps: {
6 app: { title: string; theme: 'light' | 'dark' }
7 content: { padding: string; maxWidth: string }
8}

With this configuration, setLayoutProps({ title: 'Dashboard' }) is type-checked, and setLayoutProps('app', { theme: 'dark' }) validates both the layout name and its props.

You may also pass a generic type parameter directly to setLayoutProps for ad-hoc typing without configuring the global InertiaConfig interface.

1setLayoutProps<{ custom: string }>({ custom: 'value' })
2setLayoutProps<{ collapsed: boolean }>('sidebar', { collapsed: true })
1setLayoutProps<{ custom: string }>({ custom: 'value' })
2setLayoutProps<{ collapsed: boolean }>('sidebar', { collapsed: true })

The next version of Laravel Wayfinder may automatically generate these types for you by analyzing your Laravel PHP 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";
3 
4createInertiaApp({
5 resolve: (name) => {
6 const pages = import.meta.glob<DefineComponent>("./Pages/**/*.vue");
7 return pages[`./Pages/${name}.vue`]();
8 },
9 // ...
10});
1import { createInertiaApp } from "@inertiajs/vue3";
2import type { DefineComponent } from "vue";
3 
4createInertiaApp({
5 resolve: (name) => {
6 const pages = import.meta.glob<DefineComponent>("./Pages/**/*.vue");
7 return pages[`./Pages/${name}.vue`]();
8 },
9 // ...
10});
1import { createInertiaApp, type ResolvedComponent } from "@inertiajs/react";
2 
3createInertiaApp({
4 resolve: (name) => {
5 const pages = import.meta.glob<ResolvedComponent>("./Pages/**/*.tsx");
6 return pages[`./Pages/${name}.tsx`]();
7 },
8 // ...
9});
1import { createInertiaApp, type ResolvedComponent } from "@inertiajs/react";
2 
3createInertiaApp({
4 resolve: (name) => {
5 const pages = import.meta.glob<ResolvedComponent>("./Pages/**/*.tsx");
6 return pages[`./Pages/${name}.tsx`]();
7 },
8 // ...
9});
1import { createInertiaApp, type ResolvedComponent } from "@inertiajs/svelte";
2 
3createInertiaApp({
4 resolve: (name) => {
5 const pages = import.meta.glob<ResolvedComponent>("./Pages/**/*.svelte");
6 return pages[`./Pages/${name}.svelte`]();
7 },
8 // ...
9});
1import { createInertiaApp, type ResolvedComponent } from "@inertiajs/svelte";
2 
3createInertiaApp({
4 resolve: (name) => {
5 const pages = import.meta.glob<ResolvedComponent>("./Pages/**/*.svelte");
6 return pages[`./Pages/${name}.svelte`]();
7 },
8 // ...
9});

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";
3 
4const page = usePage<{
5 posts: { id: number; title: string }[];
6}>();
7</script>
1<script setup lang="ts">
2import { usePage } from "@inertiajs/vue3";
3 
4const page = usePage<{
5 posts: { id: number; title: string }[];
6}>();
7</script>
1import { usePage } from "@inertiajs/react";
2 
3export default function Posts() {
4 const page = usePage<{
5 posts: { id: number; title: string }[];
6 }>();
7 
8 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";
2 
3export default function Posts() {
4 const page = usePage<{
5 posts: { id: number; title: string }[];
6 }>();
7 
8 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'
3 
4 const page = usePage<{
5 posts: { id: number; title: string }[]
6 }>()
7</script>
1<script lang="ts">
2 import { usePage } from '@inertiajs/svelte'
3 
4 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";
3 
4const 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";
3 
4const 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";
2 
3export 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 });
13 
14 return null;
15}
1import { useForm } from "@inertiajs/react";
2 
3export 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 });
13 
14 return null;
15}
1<script lang="ts">
2 import { useForm } from '@inertiajs/svelte'
3 
4 const 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 lang="ts">
2 import { useForm } from '@inertiajs/svelte'
3 
4 const form = useForm<{
5 name: string
6 email: string
7 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";
2 
3const 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";
2 
3const form = useForm<{
4 user: { name: string; email: string };
5 tags: { id: number; label: string }[];
6}>({
7 user: { name: "", email: "" },
8 tags: [],
9});

Form Component

The <Form> component accepts a generic type parameter for type-safe slot props. In React, you may pass the generic directly. In Vue and Svelte, you may use the createForm helper to create a typed form component.

1import { Form } from "@inertiajs/react";
2 
3interface UserForm {
4 name: string;
5 email: string;
6}
7 
8export default function CreateUser() {
9 return (
10 <Form<UserForm> action="/users" method="post">
11 {({ errors }) => (
12 <>
13 <input type="text" name="name" />
14 {errors.name && <div>{errors.name}</div>}
15 <button type="submit">Create User</button>
16 </>
17 )}
18 </Form>
19 );
20}
1import { Form } from "@inertiajs/react";
2 
3interface UserForm {
4 name: string;
5 email: string;
6}
7 
8export default function CreateUser() {
9 return (
10 <Form<UserForm> action="/users" method="post">
11 {({ errors }) => (
12 <>
13 <input type="text" name="name" />
14 {errors.name && <div>{errors.name}</div>}
15 <button type="submit">Create User</button>
16 </>
17 )}
18 </Form>
19 );
20}
1<script setup lang="ts">
2import { createForm } from "@inertiajs/vue3";
3 
4interface UserForm {
5 name: string;
6 email: string;
7}
8 
9const TypedForm = createForm<UserForm>();
10</script>
11 
12<template>
13 <TypedForm action="/users" method="post" #default="{ errors }">
14 <input type="text" name="name" />
15 <div v-if="errors.name">{{ errors.name }}</div>
16 <button type="submit">Create User</button>
17 </TypedForm>
18</template>
1<script setup lang="ts">
2import { createForm } from "@inertiajs/vue3";
3 
4interface UserForm {
5 name: string;
6 email: string;
7}
8 
9const TypedForm = createForm<UserForm>();
10</script>
11 
12<template>
13 <TypedForm action="/users" method="post" #default="{ errors }">
14 <input type="text" name="name" />
15 <div v-if="errors.name">{{ errors.name }}</div>
16 <button type="submit">Create User</button>
17 </TypedForm>
18</template>
1<script lang="ts">
2 import { createForm } from '@inertiajs/svelte'
3 
4 interface UserForm {
5 name: string
6 email: string
7 }
8 
9 const TypedForm = createForm<UserForm>()
10</script>
11 
12<TypedForm action="/users" method="post">
13 {#snippet children({ errors })}
14 <input type="text" name="name" />
15 {#if errors.name}<div>{errors.name}</div>{/if}
16 <button type="submit">Create User</button>
17 {/snippet}
18</TypedForm>
1<script lang="ts">
2 import { createForm } from '@inertiajs/svelte'
3 
4 interface UserForm {
5 name: string
6 email: string
7 }
8 
9 const TypedForm = createForm<UserForm>()
10</script>
11 
12<TypedForm action="/users" method="post">
13 {#snippet children({ errors })}
14 <input type="text" name="name" />
15 {#if errors.name}<div>{errors.name}</div>{/if}
16 <button type="submit">Create User</button>
17 {/snippet}
18</TypedForm>

The generic provides autocomplete and type checking for the errors object, setError, clearErrors, and other slot props that reference form fields.

useFormContext

The useFormContext() function also accepts a generic type parameter, providing type-safe access to the form context from child components.

1import { useFormContext } from "@inertiajs/react";
2 
3const form = useFormContext<UserForm>();
1import { useFormContext } from "@inertiajs/react";
2 
3const form = useFormContext<UserForm>();
1<script setup lang="ts">
2import { useFormContext } from "@inertiajs/vue3";
3 
4const form = useFormContext<UserForm>();
5</script>
1<script setup lang="ts">
2import { useFormContext } from "@inertiajs/vue3";
3 
4const form = useFormContext<UserForm>();
5</script>
1<script lang="ts">
2 import { useFormContext } from '@inertiajs/svelte'
3 
4 const form = useFormContext<UserForm>()
5</script>
1<script lang="ts">
2 import { useFormContext } from '@inertiajs/svelte'
3 
4 const form = useFormContext<UserForm>()
5</script>

HTTP Helper

The useHttp hook accepts two generic type parameters: the form data type and an optional default response type.

1import { useHttp } from "@inertiajs/react";
2 
3interface UserForm {
4 name: string;
5 email: string;
6}
7 
8interface UserResponse {
9 id: number;
10 name: string;
11}
12 
13const http = useHttp<UserForm, UserResponse>({ name: "", email: "" });
1import { useHttp } from "@inertiajs/react";
2 
3interface UserForm {
4 name: string;
5 email: string;
6}
7 
8interface UserResponse {
9 id: number;
10 name: string;
11}
12 
13const http = useHttp<UserForm, UserResponse>({ name: "", email: "" });

Layout Callbacks

The LayoutCallback type provides type safety for layout callbacks. The callback receives the page's props (typed via your global sharedPageProps configuration) and should return a valid layout definition.

1import type { LayoutCallback } from "@inertiajs/react";
2import Layout from "./Layout";
3 
4const Profile = () => <h1>Profile</h1>;
5 
6const callback: LayoutCallback = (props) => {
7 return [Layout, { title: props.auth.user?.name }];
8};
9 
10Profile.layout = callback;
11 
12export default Profile;
1import type { LayoutCallback } from "@inertiajs/react";
2import Layout from "./Layout";
3 
4const Profile = () => <h1>Profile</h1>;
5 
6const callback: LayoutCallback = (props) => {
7 return [Layout, { title: props.auth.user?.name }];
8};
9 
10Profile.layout = callback;
11 
12export default Profile;
1<script lang="ts">
2import Layout from "./Layout.vue";
3import type { LayoutCallback } from "@inertiajs/vue3";
4 
5const callback: LayoutCallback = (props) => {
6 return [Layout, { title: props.auth.user?.name }];
7};
8 
9export default {
10 layout: callback,
11};
12</script>
1<script lang="ts">
2import Layout from "./Layout.vue";
3import type { LayoutCallback } from "@inertiajs/vue3";
4 
5const callback: LayoutCallback = (props) => {
6 return [Layout, { title: props.auth.user?.name }];
7};
8 
9export default {
10 layout: callback,
11};
12</script>
1<script module lang="ts">
2 import Layout from './Layout.svelte'
3 import type { LayoutCallback } from '@inertiajs/svelte'
4 
5 export const layout: LayoutCallback = (props) => {
6 return [Layout, { title: props.auth.user?.name }]
7 }
8</script>
1<script module lang="ts">
2 import Layout from './Layout.svelte'
3 import type { LayoutCallback } from '@inertiajs/svelte'
4 
5 export const layout: LayoutCallback = (props) => {
6 return [Layout, { title: props.auth.user?.name }]
7 }
8</script>

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";
3 
4const 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";
3 
4const filters = useRemember<{
5 search: string;
6 status: "active" | "inactive" | "all";
7}>({
8 search: "",
9 status: "all",
10});
11</script>
1import { useRemember } from "@inertiajs/react";
2 
3export default function Users() {
4 const [filters, setFilters] = useRemember<{
5 search: string;
6 status: "active" | "inactive" | "all";
7 }>({
8 search: "",
9 status: "all",
10 });
11 
12 return null;
13}
1import { useRemember } from "@inertiajs/react";
2 
3export default function Users() {
4 const [filters, setFilters] = useRemember<{
5 search: string;
6 status: "active" | "inactive" | "all";
7 }>({
8 search: "",
9 status: "all",
10 });
11 
12 return null;
13}
1<script lang="ts">
2 import { useRemember } from '@inertiajs/svelte'
3 
4 const filters = useRemember<{
5 search: string
6 status: 'active' | 'inactive' | 'all'
7 }>({
8 search: '',
9 status: 'all',
10 })
11</script>
1<script lang="ts">
2 import { useRemember } from '@inertiajs/svelte'
3 
4 const filters = useRemember<{
5 search: string
6 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";
2 
3interface TableState {
4 sortBy: string;
5 sortDesc: boolean;
6 page: number;
7}
8 
9const restored = router.restore<TableState>("table-state");
10 
11if (restored) {
12 console.log(restored.sortBy);
13}
1import { router } from "@inertiajs/react";
2 
3interface TableState {
4 sortBy: string;
5 sortDesc: boolean;
6 page: number;
7}
8 
9const restored = router.restore<TableState>("table-state");
10 
11if (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";
2 
3interface CreateUserData {
4 name: string;
5 email: string;
6}
7 
8router.post<CreateUserData>("/users", {
9 name: "John",
10 email: "[email protected]",
11});
1import { router } from "@inertiajs/react";
2 
3interface CreateUserData {
4 name: string;
5 email: string;
6}
7 
8router.post<CreateUserData>("/users", {
9 name: "John",
10 email: "[email protected]",
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";
2 
3router.flash<{ paymentError: string }>({ paymentError: "Card declined" });
1import { router } from "@inertiajs/react";
2 
3router.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";
2 
3interface UserPageProps {
4 user: { id: number; name: string };
5}
6 
7router.push<UserPageProps>({
8 component: "Users/Show",
9 url: "/users/1",
10 props: { user: { id: 1, name: "John" } },
11});
12 
13router.replace<UserPageProps>({
14 props: (current) => ({
15 ...current,
16 user: { ...current.user, name: "Updated" },
17 }),
18});
1import { router } from "@inertiajs/react";
2 
3interface UserPageProps {
4 user: { id: number; name: string };
5}
6 
7router.push<UserPageProps>({
8 component: "Users/Show",
9 url: "/users/1",
10 props: { user: { id: 1, name: "John" } },
11});
12 
13router.replace<UserPageProps>({
14 props: (current) => ({
15 ...current,
16 user: { ...current.user, name: "Updated" },
17 }),
18});