打字稿地图,而不是{[key:string]:SomeInterface}

时间:2018-07-21 00:21:20

标签: typescript dictionary data-structures interface modeling

给出以下文字:

  user: UserConfigModel = {
    userId: 1,
    userName: "wayne",
    fullName: "Wayne Riesterer",
    email: "",
    modules: {
      skydiving: {
        moduleType: "Feature",
        name: "Skydiving",
        accessLevel: 1,
        urls: {
          details: "/skydiving/details",
          save: "/skydiving/save"
        }
      },
      paragliding: {
        moduleType: "Feature",
        name: "Paragliding",
        accessLevel: 1,
        urls: {
          details: "/paragliding/details"
        }
      },
    }
  };

和以下界面:

export interface UserConfigModel {
  userId: number;
  userName: string;
  fullName: string;
  email: string;
  modules: {[ moduleName: string]: ModuleConfigModel};
}

export interface ModuleConfigModel {
  moduleType: string;
  name: string;
  accessLevel: number;
  urls: { [ urlName: string]: string };
}

如何将以下内容转换为Map:

modules: {[ moduleName: string]: ModuleConfigModel};
urls: { [ urlName: string]: string };

我尝试了这个但没有成功:

modules: Map<string, ModuleConfigModel>;
urls: Map<string, string>;

非常感谢您的帮助!

0 个答案:

没有答案