生成嵌套对象结构$ ObjMap的流类型

时间:2019-06-29 03:57:02

标签: javascript

const fun = (name: string, val)  => {
    return {
        [name]: {
            test: () => val
        } 
    }
}  

const obj = {
    ...fun("a", "sadfsad"),
    ...fun("b", 10)
};

如何计算带流的“ obj”对象返回类型?

Link to code

1 个答案:

答案 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