似乎lodash/sortBy
错误地检测到iteratee函数(比较器)的第二个参数的类型。它涉及命名和默认lodash导入。
同时,stable
排序助手可以正确检测到它。
(沙盒:https://codesandbox.io/embed/priceless-haslett-be2h6)
问题:我可能做错了什么,还是_.sortBy与TS出现问题?
import _ from "lodash";
import sortBy from "lodash/sortBy";
import stable from "stable";
export interface ITableRow {
accountId: number;
companyName: string;
}
const rows: ITableRow[] = [];
// TS error
const sortedList1 = sortBy<ITableRow>(
rows,
(a, b) => a.accountId - b.accountId
);
// TS error
const sortedList2 = _.sortBy<ITableRow>(
rows,
(a, b) => a.accountId - b.accountId
);
// ok
const sortedList3 = stable<ITableRow>(
rows,
(a, b) => a.accountId - b.accountId
);