Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeScript接口,用于捕获具有任意数量值的对象_Typescript_Types_Structural Typing - Fatal编程技术网

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

TypeScript接口,用于捕获具有任意数量值的对象,typescript,types,structural-typing,Typescript,Types,Structural Typing,我有一个这样的物体: { "0001": "a", "0002": "b", "0003": "c", ... } interface StringInterface { [key: string]: string; } 我可以写一个描述这种类型的TypeScript接口吗?@NitzanTomer的评论完全有效:接口映射{[key:string]:string} 您还可以将其定义为文字或字符串: interface LiteralInterface {

我有一个这样的物体:

{
    "0001": "a",
    "0002": "b",
    "0003": "c",
    ...
}
interface StringInterface {
  [key: string]: string;
}

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

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

您还可以将其定义为文字或字符串:

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

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

但是在这里您必须指定每个属性…

您可以这样实现它:

{
    "0001": "a",
    "0002": "b",
    "0003": "c",
    ...
}
interface StringInterface {
  [key: string]: string;
}

除了
{[key:string]:string}