为什么导入类型会出错,而本地文件中完全相同的类型在Flow函数交集中正常

问题描述 投票:0回答:1

编辑:I reported an issue on facebook/flow.


请让我在这里问一个问题,虽然它是关于Facebook流类型的非常具体的案例。

我为类Hole写了一个复杂的类型。它的.pipe()函数不仅接受T => U而且T => Promise<U>的参数,并且两者总是返回Hole<U>

T => Promise<U>的情况下,它看起来很好,你可以在下面的例子中看到LocalType。但是,当我使用从其他文件导入的完全相同的类型时,函数交集(someone calls it Flow Conditional Types)无法在列表中找到正确的匹配项。

我完全迷失了它是否是流类型的错误或我对函数交集如何工作的误解。我不能使这个例子更具体。我想得到任何关于此的线索,所以如果你回应,我很高兴。谢谢。

// main.js
// @flow

import type {ExportedType} from './types';
type LocalType = number;

function useExported(e): Promise<ExportedType> {
  return (null: any);
}
function useLocal(e): Promise<LocalType> {
  return (null: any);
}
class Hole<T> {
  pipe<U, V>(fn: U): $Call<
      & ((T => Promise<?V>) => Hole<V>)
      & ((T => ?V) => Hole<V>),
      U> {
    return (null: any);
  }
}
function fromArray<T>(a: Array<T>): Hole<T> {
  return (null: any);
}

// Pass
(1: ExportedType);

// Pass
(1: LocalType);

// Pass
fromArray([1])
    .pipe(useLocal)
    .pipe(e => e);

// Error: ExportedType [1] is incompatible with Promise [2] in the return value.
fromArray([1])
    .pipe(useExported)
    .pipe(e => e);
// ./types.js
// @flow

export type ExportedType = number;

输出

Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ main.js:34:16

number [1] is incompatible with Promise [2] in the return value.

 [1]  6│ function useExported(e): Promise<ExportedType> {
       :
 [2] 15│       & ((T => Promise<?V>) => Hole<V>)
       :
     31│ // Error: ExportedType [1] is incompatible with Promise [2] in the return value.
     32│ fromArray([1])
     33│     .pipe(useExported)
     34│     .pipe(e => e);
     35│
     36│



Found 1 error
javascript flowtype
1个回答
0
投票

您的实现看起来不必要地复杂。尝试

class Hole<T> {
  pipe<U>(fn: (T => Promise<?U>) | (T => ?U)): Hole<U> {
    return (null: any);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.