Reputation: 6315
In my code, I want to return a high order function and flow my argument type to the returning function. The minimal reduced code looks like below.
function curry<A, B: A>(a: A): (b: B) => void {
return () => {}
}
curry(123)("123") // expected error but not
I wonder why B
does not flow to the returning function. It seems like the returning function has the type (b: any) => void
.
I know in this example I can change the type bound to a signature like (a: A) => (b: A) => void
. But my real scenario is more complicated and needs a phantom type as a bound, which looks like the B
above.
So the question is, what type is B
instantiated to? Can I make a type parameter flow to the argument position of the returning function? Can the type in argument position influence the type inference of an actual argument?
Upvotes: 1
Views: 882
Reputation: 1462
The returned function has type (b: string) => void
as you can see running the type-at-pos
command
// @flow
function curry<A, B: A>(a: A): (b: B) => void {
return () => {}
}
const f = curry(123)
f("123")
running flow type-at-pos index.js 7 7
you get:
(b: string) => void
Keep in mind that, due to how type inference works, the type A
(and thus B
) will change accordingly to the following calls
const f = curry(123) // <= f now has type (b: string | boolean) => void
f("123")
f(true)
Upvotes: 4