1

I am new to Typescript and I would like to define interfaces for the following JSON:

{  
   "company":"abc inc",
   "logoUrl":"someUrl",
   "phone":"1234567890",
   "branch":{  
      "nyc":{  
         "products":{  
            "asian":{  
               "somekey1":"someValue1",
               "somekey2":"someValue2",
               "somekeyN":"somevalueN"
            },
            "american":{  
               "somekey1":"someValue1",
               "somekey2":"someValue2",
               "somekeyN":"somevalueN"
            }
         }
      },
      "boston":{  
         "products":{  
            "asian":{  
               "somekey1":"somevalue1",
               "somekey2":"somevalue2",
               "somekeyN":"somevalueN"
            },
            "american":{  
               "somekey1":"somevalue1",
               "somekey2":"somevalue2",
               "somekeyN":"somevalueN"
            }
         }
      }
   }
}

Here is how I have currently defined my interfaces, the object asian and american can contain n number of key values. I am confused with the syntax for defining it. Can someone please guide me how I should go about this. Thanks for reading.

interface Products {
    asian: {};
    american: {};
}

interface Configuration {
    company: string;
    phone: string;
    logoUrl: string
    branch: {
      nyc: {
        products: Products;
      };
      boston: {
         products: Products;
      };
    };
}
2
  • Should asian and american be arrays? What are the keys supposed to represent? Commented Apr 12, 2018 at 21:01
  • @ExplosionPills asian and american can be used as an array, currently for this example I didn't use it. Keys can be used to represent menu item and value description. Example: friedRice: Made with....... Commented Apr 13, 2018 at 1:51

1 Answer 1

6
interface Products {
    asian: {[key: string]: string};
    american: {[key: string]: string};
}

Here I am telling typescript, that asian is a map with key = string and value = string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the syntax, this is helpful.

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.