ES6在Flowtype中映射
ES6 Map in Flowtype
什么是合适的方式处理流类型中的ecmascript-6 Map
对象?
const animals:Map<id, Animal> = new Map();
function feedAnimal(cageNumber:number) {
const animal:Animal = animals.get(cageNumber);
...
}
误差const animal:Animal = animals.get(cageNumber);
^^^^^^^^^^^^^^^^^^^^^^^^ call of method `get`
const animal:Animal = animals.get(cageNumber);
^^^^^^^^^^^^^^^^^^^^^^^^ undefined. This type is incompatible with
const animal:Animal = animals.get(cageNumber);
^^^^^^^ Animal
Flowtype Map声明
animals.get(cageNumber)
的类型是?Animal
,不是Animal
。您需要检查它是否未定义:
function feedAnimal(cageNumber:number) {
const animal = animals.get(cageNumber);
if (!animal) {
return;
}
// ...
}
相关文章:
- 如何在映射数组中添加换行符
- ng映射方向备选方案
- 无法通过数组映射绑定
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 使用Scala Play Framework视图中的键检索映射值
- 淘汰映射;不起作用
- 映射数组ES6时考虑空值
- Lodash映射并返回唯一
- 如何对映射插件创建的敲除对象进行深度复制
- 在javascript中按映射中的值排序
- 使用带有两个参数的函数的javascript映射
- 在内部映射值时渲染方法中断
- JSON:获取映射数据
- HTML Dropdownlist未映射到MVC模型
- 使用Google Maps JavaScript API v3和Geocoding API映射多个位置
- immutable.js与嵌套映射/对象的比较/相等性能
- 从映射返回React渲染数组
- 使用Google'设置ID属性;s映射API
- 使用Browserify和Typescript的源映射进行Webstorm调试
- ES6在Flowtype中映射