【发布时间】:2017-06-13 20:18:52
【问题描述】:
流程码可以是run here.
使用流,我有一个函数,它接受一个键值对对象并为其获取一个值 - 它获取的值应该是字符串、数字或布尔值。
type ValueType = string | number | bool | null | void;
type ObjectOfValues = {[string]: ValueType}
function getValueFromObjectOfValues(objectOfValues: ObjectOfValues, name: string): ValueType {
return objectOfValues[name];
}
我定义了一些具有maybe 字符串属性的对象类型:
type SomeValueWithNullableString = {
someProperty: ?string
}
然后我创建一个函数,它接受我的特定对象类型并调用该函数以从中获取值:
function getValue (someObject: SomeValueWithNullableString) {
return getValueFromObjectOfValues(someObject, 'someProperty');
}
这会导致流程错误:
type ObjectOfValues = {[string]: ValueType} ^ 布尔值。此类型与 someProperty 的预期参数类型不兼容: ?string ^ 字符串 2:类型 ObjectOfValues = {[字符串]:值类型} ^ 号。此类型与预期的参数类型 9 不兼容:someProperty: ?string ^ 字符串
我做错了什么?
【问题讨论】:
标签: javascript flowtype