【发布时间】:2021-02-04 12:18:30
【问题描述】:
我正在使用这个演示:https://codesandbox.io/s/react-virtualized-table-checbox-stackoverflow-rbl0v?fontsize=14&hidenavigation=1&theme=dark&file=/src/App.js 我把我的反应项目,我有这段代码的问题:
function _sortList({ sortBy, sortDirection }) {
const newList = _.sortBy(list, [sortBy]);
if (sortDirection === SortDirection.DESC) {
newList.reverse();
}
return newList;
}
// eslint-disable-next-line no-shadow
function _sort({ sortBy, sortDirection }) {
setSortBy(sortBy);
setSortDirection(sortDirection);
setSortedList(_sortList({ sortBy, sortDirection }));
}
变量 newList 有一个奇怪的类型(见下面的错误),不会被 setSortedList 函数接受。 我已经尝试构建一个接口,但我不确定如何使 newList 成为那种类型。 有关如何在 typescript 中使其行为的任何提示?
错误
Argument of type '(number | { id: number; code: string; title: string; status: string; assigned: string; } | { <S extends { id: number; code: string; title: string; status: string; assigned: string; }>(callbackfn: (value: { id: number; code: string; title: string; status: string; assigned: string; }, index: number, array: { ...; }[])...' is not assignable to parameter of type 'SetStateAction<{ id: number; code: string; title: string; status: string; assigned: string; }[]>’.
Type '(number | { id: number; code: string; title: string; status: string; assigned: string; } | { <S extends { id: number; code: string; title: string; status: string; assigned: string; }>(callbackfn: (value: { id: number; code: string; title: string; status: string; assigned: string; }, index: number, array: { ...; }[])...' is not assignable to type '{ id: number; code: string; title: string; status: string; assigned: string; }[]’.
Type 'number | { id: number; code: string; title: string; status: string; assigned: string; } | { <S extends { id: number; code: string; title: string; status: string; assigned: string; }>(callbackfn: (value: { id: number; code: string; title: string; status: string; assigned: string; }, index: number, array: { ...; }[]) ...' is not assignable to type '{ id: number; code: string; title: string; status: string; assigned: string; }’.
Type 'number' is not assignable to type '{ id: number; code: string; title: string; status: string; assigned: string; }'.
【问题讨论】:
标签: javascript reactjs typescript