I want to map the below JSON to a Typescript interface.
version
& localesMap
are mandatory and can occur only once.
And Config
types can be of any number, each with unique config name.
{
"version": 0.1, // <----- Mandatory, and can only occur once
"localesMap": { // <----- Mandatory, and can only occur once
"en-US": "en",
"en-GB": "en"
},
"config1": { // <----- optional, can occur more than once (key can vary)
"goto": "goto1",
"next": "next1",
"options": [
"a1",
"a2"
]
},
"myconfig2": { // <----- optional, can occur more than once (key can vary)
"goto": "goto2",
"next": "next2",
"options": [
"b1",
"b2"
]
},
// myconfig, serverconfig, clientconfig...
}
I've defined the below interfaces:
export interface Config {
goto?: string,
next?: string,
options?: string[]
}
export interface AllConfigs {
version: number,
localesMap: {
[key: string]: string
},
// <----- I want to have multiple 'Config' types here
}
All the ways I tried are resulting in TS errors. Is there a way to do this? Thanks
TS playground: link