Skip to content

TypeScript v4

Inertia Table ships with full TypeScript support. Both the Vue and React packages include type declarations that provide autocomplete, type checking, and typed slots/render props.

Importing Types

All types are exported from the main package entry point:

ts
import { Table, type TableResource, type TableColumn, type CellSlotProps } from '@inertiaui/table-vue'
tsx
import { Table, type TableResource, type TableColumn, type TableProps } from '@inertiaui/table-react'

Typed Resource Prop

Define an interface for your model and pass it as a generic to TableResource<T>. The generic flows through to slot props and render functions, so item is typed as your model:

vue
<script setup lang="ts">
import { Table, type TableResource } from '@inertiaui/table-vue'

interface User {
    id: number
    name: string
    email: string
    is_admin: boolean
}

defineProps<{
    users: TableResource<User>
}>()
</script>
tsx
import { Table, type TableResource } from '@inertiaui/table-react'

interface User {
    id: number
    name: string
    email: string
    is_admin: boolean
}

export default function Users({ users }: { users: TableResource<User> }) {
    return <Table resource={users} />
}

Typed Slots (Vue)

Passing TableResource<User> to the resource prop automatically types every slot prop. The item in cell slots is typed as User, so you get autocomplete for item.name, item.email, and so on:

vue
<Table :resource="users">
    <template #cell(name)="{ item, column, value }">
        <!-- item is typed as User; item.name, item.email autocomplete -->
        <span>{{ item.name }}</span>
    </template>

    <template #header(email)="{ column }">
        <!-- column is typed as TableColumn -->
        <span>{{ column.header }}</span>
    </template>

    <template #topbar="{ table, actions }">
        <!-- table: UseTableReturn, actions: UseActionsReturn -->
        <span v-if="table.isNavigating">Loading...</span>
    </template>
</Table>

Typed Render Props (React)

In React, the generic T is automatically inferred from the resource prop, so no explicit <Table<User>> is needed. The item in cell render functions is typed as your model:

tsx
<Table
    resource={users}
    cell={{
        name: ({ item, column, value }) => (
            // item is typed as User; item.name, item.email autocomplete
            <span>{item.name}</span>
        ),
    }}
    header={{
        email: ({ column }) => (
            <span>{column.header}</span>
        ),
    }}
    topbar={({ table, actions }) => (
        <span>{table.isNavigating ? 'Loading...' : ''}</span>
    )}
/>

Typed Callbacks

Event handlers and callbacks are also typed with your model:

vue
<script setup lang="ts">
function handleRowClick(item: User) {
    console.log(item.name) // fully typed
}
</script>

<template>
    <Table :resource="users" :on-row-click="handleRowClick" />
</template>
tsx
function handleRowClick(item: User) {
    console.log(item.name) // fully typed
}

<Table resource={users} onRowClick={handleRowClick} />

Available Types

TypeDescription
TableResource<T>The full table resource passed from PHP via Inertia props
TableItem<T>A single row item with optional _primary_key
TableColumnColumn configuration (attribute, header, sortable, toggleable, etc.)
TableFilterFilter configuration (type, clauses, options, date picker settings)
TableActionAction configuration (label, url, confirmation, variant, etc.)
TableExportExport configuration (label, url)
TableStateCurrent table state (page, sort, search, filters, columns)
UseTableReturnReturn type of useTable() (table methods and state)
UseActionsReturnReturn type of useActions() (action methods and selection)
TableSlotPropsBase slot props (table + actions)
HeaderSlotPropsHeader slot props (extends TableSlotProps with column)
CellSlotProps<T>Cell slot props (extends TableSlotProps with item, column, value)
ImageSlotProps<T>Image slot props (extends CellSlotProps with src)
TableProps<T>React Table component props (render functions, callbacks)

Composable Types

The useTable and useActions composables/hooks return typed objects:

ts
import { useTable, useActions, type UseTableReturn, type UseActionsReturn } from '@inertiaui/table-vue'
ts
import { useTable, useActions, type UseTableReturn, type UseActionsReturn } from '@inertiaui/table-react'