TypeScript generics
See where the row type of a grid is inferred, which hooks take it as a type argument, and which interfaces you extend for custom names.
The row type, TData, flows from your rows through the columns, the grid, and the hooks. TypeScript infers most of it. You pass a type argument where a hook cannot see your rows, and you extend an interface where you add a name of your own.
| Task | API | Section |
|---|---|---|
| Type the columns | defineColumns<TData>()(columns) | Inferred types |
| Type a server source | useDataGridServerRows<TData, TCursor> | Inferred types |
| Read rows inside the grid | useDataGridRow<TData>, useDataGridApi<TData>, createDataGridHooks<TData>() | Store hooks |
| Add a cell type or a keymap action | GridCellTypes, GridActions | Augmentation |
Inferred types
| API | Where TData comes from |
|---|---|
defineColumns<TData>()(columns) | You pass it. The second call types each column literal from TData: accessorKey names a row field. See ColumnDef types. |
DataGrid, DataGridProvider | Inferred from data or defaultData and from columns. getRowId, onDataChange, and onSave receive TData. |
useDataGridState(defaultRows) | Inferred from defaultRows. See Undo and redo. |
useDataGridServerRows<TData, TCursor> | You pass TData. TCursor is the cursor type of a sequential source and defaults to never. See Load without row count. |
useDataGridAggregate<TData>(specs) | Inferred from the aggregate specs, or passed. See Pinned rows. |
Store hooks
A hook that reads the store cannot see the row type of its provider. Pass the type argument yourself:
| Hook | Returns |
|---|---|
useDataGridRow<TData>(viewIndex) | TData | undefined |
useDataGridApi<TData>() | DataGridApi<TData> |
useDataGridVisibleColumns<TData>(), useDataGridAllColumns<TData>() | readonly ColumnDefOf<TData>[] |
createDataGridSelector<TData>() | A hook whose selector reads data as readonly TData[] |
Without the argument, the hooks return unknown rows. TypeScript does not compare the argument with the rows of the provider, so pass the same type that the grid has.
Declare the hooks once for your row type: export const { useDataGridRow, useDataGridApi, useDataGridSelector } = createDataGridHooks<Order>();. Each hook then reads Order rows without a type argument. Store hooks lists every hook.
Augmentation
Two interfaces in @/components/data-grid/types take names of your own:
| Interface | Adds | Page |
|---|---|---|
GridCellTypes | A cell type key with its value type and its options type. defineColumns then infers both for a column of that type. | Write a cell type |
GridActions | A keymap action name for the keymap and commands props | Custom commands |
Put the block in a file that has a top-level import or export. Only in such a file does declare module extend the installed module:
export {};
declare module "@/components/data-grid/types" {
interface GridCellTypes {
currency: {
value: number | null;
options: { currency: string; locale?: string };
};
}
interface GridActions {
saveRow: true;
}
}The module name is the import path of the installed types.ts. If your components.json aliases differ, use your path. A plugin that offers a global shortcut extends DataGridGlobalShortcutActions the same way, from @/components/data-grid/keyboard/global-shortcuts. See Plugin API.
Related
- ColumnDef: the column types
- Store hooks
- Exported types
- Define columns