【发布时间】:2021-07-01 08:50:51
【问题描述】:
我正在使用来自 antd design 的 Table。而且,我想键入onChange 函数,该函数在 Table 组件中作为参数传递。
const change = (
pagination: TablePaginationConfig,
filters: Record<string, Key[] | null>,
sorter: SorterResult<RecordType> | SorterResult<RecordType>[]
) => {};
ReactDOM.render(
<Table onChange={change} columns={columns} dataSource={data} />,
document.getElementById("container")
);
问题是我无法导入RecordType。如何在不使用any 的情况下正确键入此代码?这是demo。
【问题讨论】:
-
我没有看到 ant design 导出类型
RecordType。这只是一个打字稿泛型。您需要提供自己的类型。像这样:sorter: SorterResult<MyType> | SorterResult<MyType>[] -
@TwoHorses,你能说明一下你的意思吗?
-
为传递给
dataSourceprop 的数据创建一个类型(例如MyType)。然后在SorterResult中使用 -
@TwoHorses,但是如果在我的
change函数中我想使用例如:setData(sorter.field),如何进行。打字稿说:Property 'field' does not exist on type 'SorterResult<MyType> | SorterResult<MyType>[]'.如何解决? -
那是因为你将
sorter声明为SorterResult<MyType> | SorterResult<MyType>[],它可以是一个数组,数组没有field。
标签: reactjs typescript antd