流程:仅在已知字段上强制执行类型

时间:2017-12-06 15:50:58

标签: flowtype

我有一个Flow符号,它允许定义验证属性类型的对象类型/接口,并且未对类型检查未知属性(假设类型为any)?

const a: {x: number} = { x: 0 }
a.x = 'foo' // desirable error - x is define as number
a.y = 'bar' // undesirable error

Example in Try Flow

1 个答案:

答案 0 :(得分:1)

是的,您可以使用已知属性定义索引器属性:

const a: {x: number, [key:string]: any } = { x: 0 }

甚至更短:

const a: {x: number, [string]: any } = { x: 0 }

更多信息here