Iterate over interface properties in TypeScript

后端 未结 4 632
栀梦
栀梦 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 15:59

    if you would like to keep the interface ability you can do the following, @Nitzan Tomer is right. Interfaces are part of the type system, hence they are only relevant in compile time as they are omitted in the transpiled code.

    class Activity {
        public id: string = '';
        public title: string = '';
        public body: string = '' ;
        public json: Object = {};
    }
    
    let activity = new Activity()
    
    const headers: Array = Object.keys(Activity).map(key => {
        return { text: key, value: key }
    });
    
    console.log(JSON.stringify(headers))
    
        

    提交回复
    热议问题