const fun = (name: string, val) => {
return {
[name]: {
test: () => val
}
}
}
const obj = {
...fun("a", "sadfsad"),
...fun("b", 10)
};
如何计算带流的“ obj”对象返回类型?
答案 0 :(得分:0)
您存储函数本身,而不存储结果。需要使用括号才能得到结果:
const fun = (name, val) => {
return {
[name]: {
test: () => val
}
}
}
const obj = {
...fun("a", "sadfsad"),
...fun("b", 10)
};
console.log(obj.a.test()); // sadfsad
console.log(obj.b.test()); // 10