打字稿增强

时间:2017-08-23 17:51:24

标签: 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'声明冲突

1 个答案:

答案 0 :(得分:1)

所以我想出了如何做到这一点。问题是你无法扩充重新导出的模块。你必须直接导入它。

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