【发布时间】:2021-03-19 14:20:04
【问题描述】:
我使用@type/react-table 为我的表设置列,我的IDE 出现错误,抱怨Cell 的类型不正确。我认为它是由Cell 引起的,是@type/react-table 的可选类型我该如何解决这个问题?
//column.tsx
import {Column, Cell} from 'react-table';
export interface ColumnValue {
[key: string]: any;
}
export type TableColumn = Column<ColumnValue>
export function createColumn(colDef: TableColumn): TableColumn {
return colDef;
}
export const name = createColumn({
id: 'name',
Header: 'Name Column',
Cell({value}}) {
return value.hyperlink
},
});
//column.test.tsx
import {render} from '@testing-library/react';
import {name} from './Name';
describe('Test Name Column', () => {
it('shows the name', () => {
const {getByText} = render(
name.Cell({
// Error show TS2339: Property 'Cell' does not exist on type 'TableColumn'
value: {hyperlink: 'asadasd'}}),
})
);
expect(getByText('i am name')).toBeTruthy();
});
});
【问题讨论】:
-
查看 react-table 的类型定义,我在
Column类型上没有看到Cell,所以错误是有道理的:github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/… -
Cell在ColumnInterfaceBasedOnValue下,即ColumnWithStrictAccessor和Columngithub.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/… 的并集。但是它是可选的
标签: typescript typescript-typings typescript-generics react-table-v7 react-table-v6