Javascript 带有Union的FlowJS内联curried函数类型

Javascript 带有Union的FlowJS内联curried函数类型,javascript,types,flowtype,Javascript,Types,Flowtype,如何使用Union在流中编写内联curried函数类型 以下示例正常工作: type Foo = () => () => string; function func(foo: Foo): string { return foo()(); } 以下是工会的问题: type Foo = () => string | () => () => string; function func(foo: Foo): string { const f = foo

如何使用Union在流中编写内联curried函数类型

以下示例正常工作:

type Foo = () => () => string;

function func(foo: Foo): string {
    return foo()();
}
以下是工会的问题:

type Foo = () => string | () => () => string;

function func(foo: Foo): string {
    const f = foo();
    if (typeof f === 'function') {
      return f(); // Cannot return `f()` because function type [1] is incompatible with string [2].
    }
    return f;
}
但是,可以通过以下方式进行修复:

type TF = () => string;
type Foo = TF | () => TF;

function func(foo: Foo): string {
    const f = foo();
    if (typeof f === 'function') {
      return f();
    }
    return f;
}
那么,如何使用Union编写内联curried函数类型呢

问题在于:

type Foo = () => string | () => () => string;
目前,这意味着
Foo
是一种返回类型为:

string | () => () => string
这不是你想要的。如果您添加一些参数,flow将正确理解这一点:

type Foo = (() => string) | () => () => string;
()

问题在于:

type Foo = () => string | () => () => string;
目前,这意味着
Foo
是一种返回类型为:

string | () => () => string
这不是你想要的。如果您添加一些参数,flow将正确理解这一点:

type Foo = (() => string) | () => () => string;
()