Home > Net >  Is it possible to create json keys as typescript objects?
Is it possible to create json keys as typescript objects?

Time:10-31

Is is possible to create a Typescript interface for the following JSON where the keys of the JSON object change?

I am having a hard time defining a changing key (userFilter, activityFilter in my example) as a field name in the typescript definition

 {
  "userFilter": {
    "label": "Users",
    "options": [{
        "label": "Admin",
        "value": "ADMIN"
      },
      {
        "label": "Viewer",
        "value": "VIEWER"
      }
    ]
  },
   "activityFilter": {
     "label": "Activity",
     "options": [{
         "label": "Active",
         "value": "ACTIVE"
       },
       {
         "label": "Inactive",
         "value": "INACTIVE"
       }
     ]
   }
}

CodePudding user response:

Yes you could try something like:

interface optionsType {label:string, value:string}

interface test {
    [key:string]:{
        label:string,
        options:optionsType[]
    }
}
  • Related