Search code examples
typescriptag-gridtype-declaration

Typescript augmentation


I've been trying unsuccessfully to augment a type in agGrid with a few new properties.

import * as agGrid from 'ag-grid/main';

declare module "ag-grid/main" {
   export interface ColDef {
       format: string;
       unitType: string;
   }
}

Everything I have tried results in the original ColDef being overwritten and a build error of: Export declaration conflicts with exported declaration of 'ColDef'


Solution

  • So I figured out how to do this. The problem is you cannot augment a re-exported module. You have to directly import it.

    import ColDef from 'ag-grid/dist/lib/entities/colDef';
    // This is a patch to the ColDef interface which allows us to add new properties to it.
    declare module "ag-grid/dist/lib/entities/colDef" {
        interface ColDef {
            format?: string;
            unitType?: string;
        }
    }
    

    This will apply to any library that re-exports it's modules. In the case of agGrid there is a main.d.ts which exports its modules from other definition files which export their modules directly.

    More info here. https://github.com/Microsoft/TypeScript/issues/8427