关于在Typescript使用Table 组件的问题,基类相关

新手上路,请多包涵

antd的Table组件在d.ts中的写法如下

export interface TableProps<T> {
    dataSource?: T[];
    columns: TableColumnConfig<T>[];
}
export default class Table<T> extends React.Component<TableProps<T>, any> {
...}

如果用以下方法写Table组件

<Table rowSelection={rowSelection}
       columns={columns}
       dataSource={listState.data}/>

如果listState.data为any时不会报错,但如果有Interface时则会提示

Error:(124, 20) TS2322:Type 'DataInterface[]' is not assignable to type 'T[]'.
  Type 'DataInterface' is not assignable to type 'T'.

搜索网上其他问题,参考https://github.com/Microsoft/...,将代码改为

type CusTable = new () =>Table<DataInterface>;
const CusTable = Table as CusTable
<CusTable rowSelection={rowSelection}
          columns={columns}
          dataSource={listState.data}/>

const CusTable = Table as CusTable

中报错

TS2352:Type 'typeof Table' cannot be converted to type 'CusTable'.

求解到底应该怎么写.

阅读 13.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进