我有一个Flow符号,它允许定义验证属性类型的对象类型/接口,并且未对类型检查未知属性(假设类型为any
)?
const a: {x: number} = { x: 0 }
a.x = 'foo' // desirable error - x is define as number
a.y = 'bar' // undesirable error
答案 0 :(得分:1)
是的,您可以使用已知属性定义索引器属性:
const a: {x: number, [key:string]: any } = { x: 0 }
甚至更短:
const a: {x: number, [string]: any } = { x: 0 }
更多信息here