Typescript 打字增广

Typescript 打字增广,typescript,ag-grid,type-declaration,Typescript,Ag Grid,Type Declaration,我一直在尝试用一些新属性来扩充agGrid中的一个类型,但没有成功 import * as agGrid from 'ag-grid/main'; declare module "ag-grid/main" { export interface ColDef { format: string; unitType: string; } } 我尝试的所有操作都会导致原始ColDef被覆盖,并且生成错误:导出声明与导出的“ColDef”声明冲突,因此我找到了

我一直在尝试用一些新属性来扩充agGrid中的一个类型,但没有成功

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

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

我尝试的所有操作都会导致原始ColDef被覆盖,并且生成错误:导出声明与导出的“ColDef”声明冲突,因此我找到了解决方法。问题是您无法扩充重新导出的模块。你必须直接导入它

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;
    }
}
这将适用于任何重新导出其模块的库。对于agGrid,有一个main.d.ts,它从直接导出其模块的其他定义文件导出其模块

更多信息请点击这里。