typescript 定义一个具有 n 个键/值的对象

Ani*_*iks 1 json interface typescript

我是 Typescript 新手,我想为以下 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"
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

以下是我当前定义接口、对象的方式asian,并且american可以包含 n 个键值。我对定义它的语法感到困惑。有人可以指导我该怎么做吗?谢谢阅读。

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

interface Configuration {
    company: string;
    phone: string;
    logoUrl: string
    branch: {
      nyc: {
        products: Products;
      };
      boston: {
         products: Products;
      };
    };
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 6

interface Products {
    asian: {[key: string]: string};
    american: {[key: string]: string};
}
Run Code Online (Sandbox Code Playgroud)

在这里我告诉 typescript,asian 是一个 key = string 和 value = string 的映射。