FlowJS 内联柯里化函数类型与 Union

FlowJS inline curried function types with Union

如何在 Flow with Union 中编写内联柯里化函数类型?

下面的例子可以正常工作:

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

function func(foo: Foo): string {
    return foo()();
}

这是 Union 的问题:

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 编写内联柯里化函数类型?

Try Flow

问题在这里:

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

目前这是说 Foo 是一个 return 类型的函数类型:

string | () => () => string

这不是你想要的。如果您添加一些括号,流程将正确理解这一点:

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

(Try Flow)