我需要将接口(interface)属性映射到对象:
interface Activity {
id: string,
title: string,
body: string,
json: Object
}
我目前做的:
headers: Array<Object> = [
{ text: 'id', value: 'id' },
{ text: 'title', value: 'title' },
{ text: 'body', value: 'body' },
{ text: 'json', value: 'json' }
]
这变得非常重复。我想要的是这样的:
headers: Array<Object> = Activity.keys.map(key => {
return { text: key, value: key }
})
最佳答案
你不能,接口(interface)只用于编译时间,因为 javascript 不支持它。
你可以做的是:
const Activity = {
id: "",
title: "",
body: "",
json: {}
}
type Activity = typeof Activity;
const headers: Array<Object> = Object.keys(Activity).map(key => {
return { text: key, value: key }
});
关于javascript - 迭代 TypeScript 中的接口(interface)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670705/