Iterate over interface properties in TypeScript

后端 未结 4 639
栀梦
栀梦 2020-12-15 15:09

I need to map interface properties to objects:

interface Activity {
  id: string,
  title: string,
  body: string,
  js         


        
4条回答
  •  感动是毒
    2020-12-15 16:01

    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.keys(Activity).map(key => {
        return { text: key, value: key }
    });
    
    
    

    (code in playground)

    提交回复
    热议问题