解决 TypeScript 错误:类型“{ columns: ProColumns<CoreRoleConfigListItem>[]; editable: { type: string; editableKeys: Key[]; onSave: (rowKey: string, row: any) => Promise<void>; onChange: Dispatch<...>; actionRender: (row: any, config: any, defaultDoms: any) => any[]; }; }”分配给类型“IntrinsicAttributes & ConfigCoreTableProps & { children?: ReactNode; }”。
在使用 Ant Design Pro Table 的 EditableProTable 组件时,可能会遇到以下 TypeScript 错误:
不能将类型“{ columns: ProColumns<CoreRoleConfigListItem>[]; editable: { type: string; editableKeys: Key[]; onSave: (rowKey: string, row: any) => Promise<void>; onChange: Dispatch<...>; actionRender: (row: any, config: any, defaultDoms: any) => any[]; }; }”分配给类型“IntrinsicAttributes & ConfigCoreTableProps & { children?: ReactNode; }”。
类型“IntrinsicAttributes & ConfigCoreTableProps & { children?: ReactNode; }”上不存在属性“editable”。ts(2322)
类型“IntrinsicAttributes & { children?: ReactNode; }”上不存在属性“columns”。ts(2322)
该错误的原因是,EditableProTable 组件需要接收 columns 和 editable 属性,而您的 ConfigCoreTable 组件的类型定义中没有包含这两个属性。
为了解决这个问题,您需要在 ConfigCoreTable 组件的类型定义中添加 columns 和 editable 属性。可以通过 interface 或 type 来完成:
import { ProColumns } from '@ant-design/pro-table';
import { Dispatch, ReactNode } from 'react';
interface ConfigCoreTableProps {
columns: ProColumns[];
editable: {
type: string;
editableKeys: string[];
onSave: (rowKey: string, row: any) => Promise<void>;
onChange: Dispatch<string[]>;
actionRender: (row: any, config: any, defaultDoms: any) => ReactNode[];
};
children?: ReactNode;
}
const ConfigCoreTable: React.FC<ConfigCoreTableProps> = ({ columns, editable, ...otherProps }) => {
return (
<EditableProTable
{...otherProps}
columns={columns}
editable={editable}
rowKey='id'
options={false}
scroll={{ y: 600 }}
cardProps={{
bodyStyle: {
padding: 0,
},
}}
recordCreatorProps={false}
bordered
/>
);
};
export default ConfigCoreTable;
然后在使用 ConfigCoreTable 组件时,需要传递 columns 和 editable 属性,例如:
<ConfigCoreTable
columns={columns}
editable={{
type: 'multiple',
editableKeys,
onSave: async (rowKey, row) => {
// ...
},
onChange: setEditableKeys,
actionRender: (row, config, defaultDoms) => {
return [defaultDoms.save, defaultDoms.cancel];
},
}}
/>
通过添加这两个属性,您的代码就可以正常编译并运行了。
原文地址: https://www.cveoy.top/t/topic/n5tc 著作权归作者所有。请勿转载和采集!