这是一个SSCCE http://sscce.org/.
我有一个地图容器类,其中内部Map
是根据需要创建的set
被调用的方法:
// @flow
'use strict';
class MapContainer {
map: ?Map<any, any>;
constructor() {
this.map=null;
}
set(key: any, value: any): ?any {
if (this.map===null) {
this.map = new Map();
}
let prevValue: ?any;
if (this.map!=null) { // first check
prevValue = this.map.get(key);
}
if (this.map!=null) { // second check
this.map.set(key, value);
}
return prevValue;
}
}
exports.MapContainer = MapContainer;
上面的代码通过了npm run flow
没有任何警告。
但是如果我将两者合并if (this.map!=null)
检查其中之一:
// @flow
'use strict';
class MapContainer {
map: ?Map<any, any>;
constructor() {
this.map=null;
}
set(key: any, value: any): ?any {
if (this.map===null) {
this.map = new Map();
}
let prevValue: ?any;
if (this.map!=null) { // merged check
prevValue = this.map.get(key);
this.map.set(key, value);
}
return prevValue;
}
}
exports.MapContainer = MapContainer;
…然后运行流程失败并显示以下消息:
es6/map-container.js:19
19: this.map.set(key, value);
^^^^^^^^^^^^^^^^^^^^^^^^ call of method `set`. Method cannot be called on possibly null value
19: this.map.set(key, value);
^^^^^^^^ null
es6/map-container.js:19
19: this.map.set(key, value);
^^^^^^^^^^^^^^^^^^^^^^^^ call of method `set`. Method cannot be called on possibly undefined value
19: this.map.set(key, value);
^^^^^^^^ undefined
…这对于第 19 行的访问来说完全没有意义:
this.map.set(key,value)
…仍然在支票范围内:
if (this.map!=null)
是什么赋予了?