Framework:Javascript Data GridAngular Data GridReact Data GridVue Data Grid

Angular Data Grid: Typescript Generics

AG Grid supports Typescript Generics for row data and cell values. This leads to greatly improved developer experience via code completion and compile time validation of row data and cell value properties.

Row Data: <TData>

Provide a Typescript interface for row data to the grid to enable auto-completion and type-checking whenever properties are accessed from a row data variable. There are multiple ways to configure the generic interface: via the GridOptions<TData> interface, via other individual interfaces and finally via framework components.

In the examples below we will use the ICar interface to represent row data.

// Row Data interface
interface ICar {
    make: string;
    model: string;
    price: number;
}

Configure via GridOptions

Set the row data type on the grid options interface via GridOptions<ICar>. The ICar interface will then be used throughout the grid options whenever row data is present. This is true for: properties, callbacks, events and the gridApi.

// Pass ICar to GridOptions as a generic
const gridOptions: GridOptions<ICar> = {
    // rowData is typed as ICar[]
    rowData: [ { make: 'Ford', model: 'Galaxy', price: 20000 } ],

    // Callback with params type: GetRowIdParams<ICar>
    getRowId: (params) => {
        // params.data : ICar
        return params.data.make + params.data.model;
    },

    // Event with type: RowSelectedEvent<ICar>
    onRowSelected: (event) => {
        // event.data: ICar | undefined
        if (event.data) {
            const price = event.data.price;
        }
    }
}

// Grid Api methods use ICar interface
function onSelection() {
  // api.getSelectedRows() : ICar[]
  const cars: ICar[] = gridOptions.api!.getSelectedRows();  
}

You do not need to explicitly type callbacks and events that are defined as part of GridOptions. Typescript will correctly pass the generic type down the interface hierarchy.

Configure via Interfaces

Each interface that accepts a generic type of TData can also be configured individually. For example, an event handler function can accept the generic parameter on the event RowSelectedEvent.

function onRowSelected(event: RowSelectedEvent<ICar>) {
    if (event.data) {
        // event.data: ICar | undefined
        const price = event.data.price;
    }
}

Configure via Component

The <ag-grid-angular> component is generic with respect to TData. As there is no way to explicitly set generic parameters on Angular components, Angular infers the TData generic type from Inputs / Outputs. However, it will not always be successful, leaving TData inferred as any.

We recommend providing the generic TData type to your columnDefs property as in practice we see this leading to the most accurate inference.

To provide the generic type of ICar to the columnDefs property you use the type ColDef<ICar> as follows:

const columnDefs: ColDef<ICar>[] = [...];

Then set this on your component template as [columnDefs]="columnDefs". This provides Angular with the information it needs to assigns ICar to TData for the component.

<ag-grid-angular 
   [columnDefs]="columnDefs"    
   (rowSelected)="onRowSelected($event)"
></ag-grid-angular>

This generic parameter is used for all other Inputs and Outputs ensuring consistency across the component. If (rowSelected) is defined with a different interface the application code will fail to compile.

// ERROR: INotACar is not assignable to ICar
onRowSelected(event: RowSelectedEvent<INotACar>) {}

// SUCCESS: ICar is correct interface
onRowSelected(event: RowSelectedEvent<ICar>) {}

Type: TData | undefined

For a number of events and callbacks, when a generic interface is provided, the data property is typed as TData | undefined instead of any. The undefined is required because it is possible for the data property to be undefined under certain grid configurations.

A good example of this is Row Grouping. The onRowSelected event is fired for both leaf and group rows. Data is only present on leaf nodes and so the event should be written to handle cases when data is undefined for groups.

function onRowSelected(event: RowSelectedEvent<ICar>) {
    // event.data is typed as ICar | undefined
    if (event.data) {
        // Leaf row with data
        const price = event.data.price;
    } else {
        // This is a group row
    }
}

Cell Value: <TValue>

When working with cell values it is possible to provide a generic interface for the value property. While this will often be a primitive type, such as string or number, it can also be a complex type. Using a generic for the cell value will enable auto-completion and type-checking.

Configure via Interfaces

The generic parameter TValue needs to be explicitly provided to each interface. Here is an example of a valueFormatter for the price column. The params.value property is correctly typed as a number due to typing the params argument as ValueFormatterParams<ICar, number>.

const colDefs: ColDef<ICar>[] = [
     {
        field: 'price',
        valueFormatter: (params: ValueFormatterParams<ICar, number>) => {
            // params.value : number
            return "£" + params.value;
        }
    }
];

The TValue generic type is also supported for cell renderers / editors by ICellRendererParams<TData, TValue> and ICellEditorParams<TData, TValue> respectively.

Typed: TValue | undefined

For a number of events and callbacks when a generic interface is provided the value property is typed as TValue | undefined instead of any. This is because it is possible for the value property to be undefined under certain grid configurations.

Generic Type Example

Inspect the code in the following example or open in Plunker to experiment with generic typing yourself.

Fallback Default

If generic interfaces are not provided then the grid will use the default type of any. This means that generics in AG Grid are completely optional. GridOptions is defined as GridOptions<TData = any>, so if a generic parameter is not provided then any is used in its place for row data properties.

Likewise for cell values, if a generic parameter is not provided, any is used for the value property. For example, cell renderer params are defined as ICellRendererParams<TData = any, TValue = any>.