打字稿扩充

Typescript augmentation

我一直在尝试使用一些新属性来扩充 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 从直接导出其模块的其他定义文件中导出其模块。

更多信息请点击此处。 https://github.com/Microsoft/TypeScript/issues/8427