sAs59
sAs59

Reputation: 572

FlowJS inline curried function types with Union

How to write inline curried function types in Flow with Union?

The following example works ok:

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

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

Here is the problem with 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;
}

But, it can be fixed by doing:

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

function func(foo: Foo): string {
    const f = foo();
    if (typeof f === 'function') {
      return f();
    }
    return f;
}

So how can I write inline curried function types with Union?

Try Flow

Upvotes: 0

Views: 58

Answers (1)

Lyle Underwood
Lyle Underwood

Reputation: 1304

The problem is here:

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

Currently this is saying that Foo is a function type with a return type of:

string | () => () => string

Which is not what you want. If you add some parens, flow will make proper sense of this:

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

(Try Flow)

Upvotes: 2

Related Questions