flow:将混合类型转换为一个形状数组

时间:2018-01-22 23:15:24

标签: flowtype mixed

我需要在流程中将混合类型转换为形状数组。 Tryflow link

type aShapedArray = Array<{a:string}>;

 //externally defined type
const transform = ():mixed => [{a: 'hello'}];

const b = transform();
if (Array.isArray(b)) {
    const a: aShapedArray = b;
}

错误是

9:  const a: aShapedArray = b;
                            ^ array. Has some incompatible type argument with
9:  const a: aShapedArray = b;
             ^ aShapedArray
Type argument `T` is incompatible:
7: const b = transform();
             ^ mixed. This type is incompatible with
2: type aShapedArray = Array<{a:string}>;
                             ^ object type

1 个答案:

答案 0 :(得分:0)

可悲的是,我找到的唯一答案是向数组添加any强制转换。这几乎相当于flow: ignore this line

/* @flow */

type aShapedArray = Array<{a:string}>;

//externally defined type
const transform = ():mixed => [{a: 'hello'}];

const b: any = transform();
if (Array.isArray(b)) {
  const a: aShapedArray = b;
}

尝试流程:link