在 TypeScript 中,如何修改第三方接口?

In TypeScript, how do I modify a third party interface?

目前,我正在使用以下GridOptions界面:

https://github.com/ag-grid/ag-grid/blob/f065d96ee682d4c43a7275f35935d122d9faeee6/packages/ag-grid-community/dist/lib/entities/gridOptions.d.ts

根据 API:

https://www.ag-grid.com/javascript-grid-events/

onFirstDataRendered应该存在于界面上,但没有。

在我的代码中,我使用的接口如下:

import { GridOptions } from "ag-grid/main";
private gridOptions: GridOptions = <GridOptions>{};

如何修改界面以使 onFirstDataRendered 可用?

编辑:

API页面有如下注释:

This is done by prefixing the event name with 'on', for instance gridOptions.onCellClicked.

例如,您可以看到 API 中的 gridReady 在以下代码中转换为 onGridReady

https://github.com/ag-grid/ag-grid/blob/f065d96ee682d4c43a7275f35935d122d9faeee6/packages/ag-grid-community/dist/lib/entities/gridOptions.d.ts

试试 "firstDataRendered" 而不是 "onFirstDataRendered"?

编辑:

嗯。似乎他们有一些错误,因为 "onFirstDataRendered" 和 "firstDataRendered" does not appear in GridOptions interface, unlike they promised here "All of these grid events are available through the GridOptions interface."

顺便说一句 here 你可以看到事件常量,让你的生活更轻松一些(至少你不会输入错误的事件名称)。

为了扩展接口你必须这样做:

private gridOptions: GridOptions & {onFirstDataRendered: any} = {}; // "any" because i don't know what type the value should have. I removed the type guard (<GridOptions>) because it is redundant, moreover typeguards are deprecated in favor of "as GridOptions" syntax.

P.S。是的,另一个人说这是一个新功能。但是它是在 npm (v.19.0.0) 上发布的。但它看起来有问题(不在界面中)。您应该在他们的 github 存储库中创建一个关于此的问题。