Typescript 如何使用预定义值初始化字典?

Typescript 如何使用预定义值初始化字典?,typescript,Typescript,我需要创建一个预定义的字典,其中键是一个城市,值是该城市中的一个区域数组。我试过类似的东西 export const cityToZone: { [city: string]: Array<string> } = [ {city:'New York', ['zoneA','ZoneB']} ] 我是typescript新手,我不知道这里出了什么问题。欢迎来到typescript社区。如果我需要获取某些城市的所有区域,我是否需要通过cityToZone数组查找我要查找的城市

我需要创建一个预定义的字典,其中键是一个城市,值是该城市中的一个区域数组。我试过类似的东西

export const cityToZone: { [city: string]: Array<string> } = [

    {city:'New York', ['zoneA','ZoneB']}
]

我是typescript新手,我不知道这里出了什么问题。

欢迎来到typescript社区。如果我需要获取某些城市的所有区域,我是否需要通过cityToZone数组查找我要查找的城市,然后检索区域?我已更新了我的答案,以解决您的问题
(TS) Type '{ city: string; ['zoneA','ZoneB']: any; }' is not assignable to type 'string[]'.  Object literal may only specify known properties, and 'city' does not exist in type 'string[]'.
type City = {
  city: string;
  zones: string[];
};
export const cityToZone: City[] = [{
  city: 'New York', 
  zones: ['zoneA','ZoneB']
}];
type CityName = 'New York' | 'Denver' | 'Washington, D.C.'; // etc
type ZoneName = 'Zone 1' | 'Zone 2' | 'Zone 3'; // etc
type City = {
  city: CityName;
  zones: ZoneName[];
}
type CityMap = Record<CityName, ZoneName[]>;
export const cityToZone: CityMap = {
  'New York': ['zoneA', 'zoneB'],
  'Denver': ['zoneC', 'zoneD'],
};