首页 > 解决方案 > Typescript - 具有任意数量键值的对象

问题描述

我正在尝试编写一个接口来处理这样的数据:

interface SessionList {
   appInfo: Object(string: string | null);
}

appInfo 下可能有 0 到多个项目。我正在尝试找到正确的语法,但没有运气 - 目前在:

appInfo: Object(string: string | null);

有任何想法吗?

标签: typescript

解决方案


任何一个

interface AppInfo {
  [key: string]: string | null;
}

或者

Record<string, string | null>

更新:

let obj: { appInfo: Record<string, string | null> };

interface SessionList {
   appInfo: Record<string, string | null>;
}

let obj: SessionList;

推荐阅读