I need to map interface properties to objects:
interface Activity { id: string, title: string, body: string, json: Object }
I currently do:
headers: Array<Object> = [ { text: 'id', value: 'id' }, { text: 'title', value: 'title' }, { text: 'body', value: 'body' }, { text: 'json', value: 'json' } ]
This gets very repetitive. What I would like is something like this:
headers: Array<Object> = Activity.keys.map(key => { return { text: key, value: key } })
You can't, interfaces are only for compile time because javascript doesn't support it.
What you can do is something like:
const Activity = { id: "", title: "", body: "", json: {} } type Activity = typeof Activity; const headers: Array<Object> = Object.keys(Activity).map(key => { return { text: key, value: key } });
(code in playground)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With