export function transDictToArray(
dict: Record<string, unknown>,
keyLabel = "value",
valueLabel = "label"
): Array<{
[keyLabel]: string;
[valueLabel]: string;
}> {
const arr = [];
for (const key in dict) {
if (Object.prototype.hasOwnProperty.call(dict, key)) {
arr.push({
[keyLabel]: key,
[valueLabel]: dict[key],
});
}
}
return arr;
}
报错:类型文本中的计算属性名称必须引用类型为文本类型或 "unique symbol" 类型的表达式。ts(1170)
ts不能用变量作为返回值吗
那就要用到类型世界里的参数----泛型了啦。
不考虑默认值问题会比较简单: TS Playground
考虑默认参数的话,只能用重载实现了:
TS Playground