0
如何键入一个函数,以便输入对象与输出对象相同,但具有不同的值?打字稿对象转换
//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);
没有这不会工作,因为我会叫用'{“C转换':23,'d':45}'然后不能恢复正确的类型 – Cine