尝试在 AG 网格上做一个项目,但我遇到了 typescript 错误。它说([AG-Grid] 类型“ColDef”上不存在属性“children”)在下面的过滤器方法中出现此错误(column => column.children)。
我查看了 ag-grid 的文档,但我可以看到列定义支持使用子项作为属性。所以我不确定为什么会收到此错误?
import { ColDef, ColGroupDef } from 'ag-grid-community';
export const sampleItems: Array<{
label: string;
value: string;
}> = [];
export const TABLE_COLUMN = [
{
headerName: 'User',
children: [
{
field: 'user',
headerName: 'Name',
pinned: 'left',
type: 'fixed',
},
{ field: 'account', pinned: 'left', type: 'fixed' },
{
field: 'price',
headerName: 'Pricing',
pinned: 'left',
type: 'primary',
},
],
},
{
headerName: 'Address',
children: [
{ field: 'address', headerName: 'Address', type: 'secondary' },
{
field: 'street',
headerName: 'Street Name',
columnGroupShow: 'closed',
type: 'secondary',
},
],
},
] as (ColDef<object> | ColGroupDef<object>)[];
TABLE_COLUMN.filter((column) => column.children != null).forEach(
(column) => {
column.children.forEach((child) => {
if (child.type === 'fixed' || child.type === 'primary') {
sampleItems.push({
label:
child.headerName ??
child.field.charAt(0).toUpperCase() + child.field.slice(1),
value: child.field,
});
}
});
},
);
最佳答案
将逻辑更改为。我们告诉 typescript 它将是一个 colGroupDef
,因此它不会费心检查 ColDef
类型。
(<ColGroupDef<object>>TABLE_COLUMN).filter((column: ColGroupDef<object>) => column.children != null).forEach(
(column: ColGroupDef<object>) => {
column.children.forEach((child: ColDef<object>) => {
if (child.type === 'fixed' || child.type === 'primary') {
sampleItems.push({
label:
child.headerName ??
child.field.charAt(0).toUpperCase() + child.field.slice(1),
value: child.field,
});
}
});
},
);
关于typescript - <AG-Grid> 类型 'children' 上不存在属性 'ColDef<object>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73454720/