[英]FlowJS inline curried function types with Union
How to write inline curried function types in Flow with Union? 如何在Flow with Union中编写内联咖喱函数类型?
The following example works ok: 以下示例可以正常工作:
type Foo = () => () => string;
function func(foo: Foo): string {
return foo()();
}
Here is the problem with Union: 这是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? 那么,如何用Union编写内联咖喱函数类型呢?
The problem is here: 问题在这里:
type Foo = () => string | () => () => string;
Currently this is saying that Foo
is a function type with a return type of: 当前这是说Foo
是具有以下返回类型的函数类型:
string | () => () => string
Which is not what you want. 这不是您想要的。 If you add some parens, flow will make proper sense of this: 如果添加一些括号,那么流程将适当地说明这一点:
type Foo = (() => string) | () => () => string;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.