【发布时间】:2020-12-30 14:00:06
【问题描述】:
我有这个接口的对象数组
interface Item {
country: string;
cases: number;
deaths: number;
population: number;
}
我想做排序功能
const sortDescending = (type: keyof Item): void => {
data.sort((a, b) => {
return a[type] - b[type];
});
};
打字稿不允许我执行它的问题是因为“算术运算的左侧必须是'any'、'number'、'bigint'或枚举类型”我知道我有对象中的字符串,我不能对它们进行算术运算。如何解决?
【问题讨论】:
-
那你想做什么?将排序功能限制为数字属性,或者改进排序功能使其也适用于字符串?
标签: typescript