TypeScript索引签名中“[key:string]”和“[key-in-string]”之间的差异

TypeScript索引签名中“[key:string]”和“[key-in-string]”之间的差异,typescript,Typescript,考虑以下使用:和中的定义索引签名的示例: interface Colon { [key : string]: number; } interface ColonOpt { [key : string]?: number; // does not compile } interface In { [key in string]: number; // does not compile } interface InNested { a: { [key in string

考虑以下使用
中的
定义索引签名的示例:

interface Colon {
  [key : string]: number;
}

interface ColonOpt {
  [key : string]?: number; // does not compile
}

interface In {
  [key in string]: number; // does not compile
}

interface InNested {
  a: {
    [key in string]: number;
  };
}

interface InNestedOpt {
  a: {
    [key in string]?: number;
  };
}
我有点困惑,为什么有些编译,有些不编译。在
中使用
与在索引签名中使用
有什么区别?

因为,你在创建,这对
[输入字符串]
没有多大意义。因为,你在创建,对
[输入字符串]
没有多大意义。