为什么 Flow 在使用 isMap() 检查后不能告诉我我的对象是一个不可变映射?
Why can't Flow tell that my object is an Immutable Map after checking with isMap()?
我正在尝试找出为什么 Flow 在此处出错:
// @flow
import { Map } from 'immutable';
type Filemap = Map<string, Buffer>;
type FilemapCompatibleObject = Filemap | { [string]: Buffer };
export const castFilemap = (object: FilemapCompatibleObject): Filemap => {
// checking with instanceof works fine
if (object instanceof Map) {
return object;
}
// checking with isMap does not
if (Map.isMap(object)) {
return object;
// ^^^ FLOW ERROR: object type This type is incompatible with the expected return type of Map
}
return Map(object);
};
检查 Map.isMap(object)
后,我知道该对象是一个 Map,但 Flow 不是。为什么不?有什么方法可以 'inform' 让它现在是一张地图吗?
可能的子问题:与 obj instanceof Map
相比,使用 Map.isMap(obj)
实际上有什么优势吗? (如果没有,为什么他们提供静态方法?)
编辑:查看 Immutable's own typedef 它似乎有一些有趣的 %checks
语法来解决我遇到的问题,但它似乎对我不起作用。我找不到关于它的文档。
declare function isMap(maybeMap: mixed): boolean %checks(maybeMap instanceof Map);
回答我自己的问题...
看来问题是 %checks
尚未在 Immutable 中发布。最新稳定版 3.8.1 doesn't have it.
而且它似乎还没有稳定在 Flow 中(从 v0.45 开始)。
我正在尝试找出为什么 Flow 在此处出错:
// @flow
import { Map } from 'immutable';
type Filemap = Map<string, Buffer>;
type FilemapCompatibleObject = Filemap | { [string]: Buffer };
export const castFilemap = (object: FilemapCompatibleObject): Filemap => {
// checking with instanceof works fine
if (object instanceof Map) {
return object;
}
// checking with isMap does not
if (Map.isMap(object)) {
return object;
// ^^^ FLOW ERROR: object type This type is incompatible with the expected return type of Map
}
return Map(object);
};
检查 Map.isMap(object)
后,我知道该对象是一个 Map,但 Flow 不是。为什么不?有什么方法可以 'inform' 让它现在是一张地图吗?
可能的子问题:与 obj instanceof Map
相比,使用 Map.isMap(obj)
实际上有什么优势吗? (如果没有,为什么他们提供静态方法?)
编辑:查看 Immutable's own typedef 它似乎有一些有趣的 %checks
语法来解决我遇到的问题,但它似乎对我不起作用。我找不到关于它的文档。
declare function isMap(maybeMap: mixed): boolean %checks(maybeMap instanceof Map);
回答我自己的问题...
看来问题是 %checks
尚未在 Immutable 中发布。最新稳定版 3.8.1 doesn't have it.
而且它似乎还没有稳定在 Flow 中(从 v0.45 开始)。