使用TypeScript在lodash / sortBy中错误的类型检测

时间:2019-07-10 10:25:06

标签: arrays typescript sorting lodash

似乎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
);

0 个答案:

没有答案