From a legacy api I am getting a JSON response like so:
const someObject = {
"general": {
"2000": 50,
"4000": 100,
"8000": 200,
},
"foo": [
0,
1,
2,
],
"bar": [
5,
7,
],
"baz": [
8,
9,
],
};
Keep in mind that all the indexes except "general" are dynamic and might not be in the response, I cannot type for each property but have to use an index signature.
I wanted to achieve that via typescript@2.9.2:
interface ISomeObject {
general: {
[index: string]: number;
};
[index: string]?: number[];
}
as general
will always be in the response, yet the other indexes might or might not be in there.
Issue that I am facing:
- I cannot make the
[index: string]?: number[]
optional as it will complain that number is used as a value here. [index: string]: number[]
will override the definition ofgeneral: number
and hence tsc will complain:Property 'general' of type '{ [index: string]: number; }' is not assignable to string index type 'number[]'.`
Can I even type for this format with a TypeScript interface?