0

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"
       }
     ]
   }
}
1

1 Answer 1

1

Yes you could try something like:

interface optionsType {label:string, value:string}

interface test {
    [key:string]:{
        label:string,
        options:optionsType[]
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.