用于捕获具有任意数量值的对象的 TypeScript 接口

TypeScript interface to catch objects with any amount of values

我有一个这样的对象:

{
    "0001": "a",
    "0002": "b",
    "0003": "c",
    ...
}

我可以写一个 TypeScript 接口来描述这个类型吗?

@NitzanTomer 的评论完全有效:接口映射 { [key: string]: string }

您也可以将其定义为文字,或仅定义为字符串:

interface LiteralInterface { 
    "0001": "a",
    "0002": "b",
    "0003": "c",
     ...
}

interface StringInterface { 
    "0001": string,
    "0002": string,
    "0003": string,
     ...
}

但在这里你必须指定每个 属性...

你可以这样实现:

interface StringInterface {
  [key: string]: string;
}