Typescript 对象的对象的类型脚本类型?

Typescript 对象的对象的类型脚本类型?,typescript,Typescript,正确的类型是什么 const test: MyType = { foo: { value: 1 }, bar: { value: 2 } } type MyType { [key: any]: { value:number } } 差不多 const test: MyType = { foo: { value: 1 }, bar: { value: 2 } } type MyType { [key: any]: { value:number

正确的类型是什么

const test: MyType = {
  foo: { value: 1 },
  bar: { value: 2 }
}
type MyType {
  [key: any]: {
    value:number
  }
}
差不多

const test: MyType = {
  foo: { value: 1 },
  bar: { value: 2 }
}
type MyType {
  [key: any]: {
    value:number
  }
}
导致

Object literal may only specify known properties, and 'foo' does not exist in type 

索引签名参数类型必须为字符串或数字:

或者,使用:


索引签名参数类型必须为字符串或数字:

或者,使用:


您可以添加更多信息来解释为什么应该使用记录而不是类型。您可以添加更多信息来解释为什么应该使用记录而不是类型。您的问题似乎不正确,您的类型签名没有正确声明,实际上会给您一个索引签名参数类型必须是“string”或“number”错误,即使声明正确。您的问题似乎不正确,您的类型签名没有正确声明,实际上会给您一个索引签名。参数类型必须是“string”或“number”错误,即使它被正确声明。
const test: Record<string, {value: number}> = {
    foo: { value: 1 },
    bar: { value: 2 }
};