I am having a hard time understanding how the object types are formed given a JSON. For example I have this JSON object which I need to form a type for.
// "Spokane, WA": {
// "My M": "M-Spokane-WA.json",
// "MY D": "D-Spokane-WA.json"
// },
// "Pondera, MT": {
// "My M": "M-Pondera-MT.json",
// "MY D": "D-Pondera-MT.json"
// },
The rule I am following is copy the JSON while creating the object and then give the names to the variables. In the end I have something like this.
export interface IStateType {
cityState: string { // "Spokane, WA" is in quotes so seems like is coming in string.
My M :string // "My M" is string .. How to show the "M-Spokane-WA.json" part???
MY D: string
}
}
turned out this is the correct way to put it
export interface IStateType {
[cityState: string]: {
["My M"]: string;
["MY D"]: string;
};
}
Can someone please explain why are we using [] for cityState and why are there quotes around My M and My D but not around cityState?
--------------- Udpate 2 From the link on index signature
interface StringArray {
[index: number]: string; // It says string is a return type..
}
let myArray: StringArray;
myArray = ["Bob", "Fred"]; // Is this the return mentioned above :string?
let myStr: string = myArray[0]; // Is myArray[0] this the [index:number] part of interfaces StringArray?
string(lowercase). I'd guess it's TypeScript (or possibly some related derivative)... but you should know better than me what language you're using.My Mon its own isn't a valid JavaScript identifier, so it has to be in quotes.