next.js 为什么onChange中的selectedRowKeys不是一个数字数组?

xbp102n0  于 2023-05-06  发布在  其他
关注(0)|答案(1)|浏览(94)

我在一个Nextjs项目中使用typescriptantd
我使用的是antd表,用户可以选择行。

const rowSelection = {
  onChange: (selectedKeys: any[], selectedRows: Mission[]) => {
    console.log(selectedKeys);
  }
}

结果:[12, 11, 10]
这是一个数字数组。
但如果换成selectedKeys: number[]

Type '{ onChange: (selectedKeys: number[], selectedRows: Mission[]) => void; getCheckboxProps: (record: Mission) => { disabled: boolean; }; }' is not assignable to type 'TableRowSelection<Mission>'.
  Types of property 'onChange' are incompatible.
    Type '(selectedKeys: number[], selectedRows: Mission[]) => void' is not assignable to type '(selectedRowKeys: Key[], selectedRows: Mission[], info: { type: RowSelectMethod; }) => void'.
      Types of parameters 'selectedKeys' and 'selectedRowKeys' are incompatible.
        Type 'Key[]' is not assignable to type 'number[]'.
          Type 'Key' is not assignable to type 'number'.
            Type 'string' is not assignable to type 'number'.ts(2322)
Table.d.ts(22, 5): The expected type comes from property 'rowSelection' which is declared here on type 'IntrinsicAttributes & TableProps<Mission> & { children?: ReactNode; } & { ref?: Ref<HTMLDivElement> | undefined; }'

为什么?

flseospp

flseospp1#

查看rowSelection上的antd文档,onChange接受3个参数,分别是selectedRowKeysselectedRowsinfo:{ type }。如果你再读一遍selectedRowKeys,类型是string[]|number[]
显然,如果您键入any[],则不会有任何问题。但是如果你输入selectedRowKeys[]作为number[],你可能会遇到错误,因为onChange可能会被调用,而值不是数字(例如字符串数组)。
TLDR,尝试selectedKeys = string[]|number[]代替。

相关问题