【发布时间】:2017-03-22 20:44:03
【问题描述】:
如何键入函数,使输入对象与输出对象相同,但值不同?
//a has type { a: number;b: number }
let a = { 'a': 1, 'b': 1 };
interface IDictNumber {
[key: string]: number;
}
interface IDictString {
[key: string]: string;
}
function convert(f: IDictNumber) {
return Object.keys(f)
.reduce((p, v) => {
p[v] = `${f[v]}`;
return p;
},
{} as IDictString);
}
//b has type IDictString, but I wanted it to have { a: string;b: string }
let b= convert(a);
【问题讨论】:
标签: typescript typescript2.0 typescript2.1