facebook / facebook/flow

Unexpected type errors when wrapping a generic function type

Open
#5,149 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

Not really sure how to title this more clearly, but here's the repro. Sorry if there's something obvious I'm missing here.

https://flow.org/try/#0PTAEAEDMBsHsHcBQiAuBPADgU1AESwM4DGATgJYYqwkA8AKgHygC8oA3gL4DcqmOAMrACGAEywj8xcpWot23RJACuAOyIoysFaHgkhGGgEESAcwYAKSCoBcoYyZZMAbrDIiAlLfuPQLt+0RQUBIsFCUSbSseDmRlNQ0tUDhRACEhAjIiegtYACMAK1tJUgoqWkZPX1cReWQiLQIUJOERAHU9DGwa1l19c2SRNIyidx4g8fHFVXVNbQHs8zzCvEISmXKGdwCJgfb9LsWC0dAQUCwSEmoCQPGBoczD-OPT+GoAa2uYoA

```
type Descriptor = {};
type LoadedDescriptor = {};
function wrap(fn: Arg => void): Arg => void {
return fn;
}

function loadBasic(obj: Descriptor): void {}

const loadWrapped = wrap(loadBasic);

function load(obj: Descriptor) {
loadWrapped(obj); // errors
loadBasic(obj); // works
}
```
resulting in
```
11: const loadWrapped = wrap(loadBasic);
^ function. This type is incompatible with the expected param type of
5: function wrap(fn: Arg => void): Arg => void {
^ function type
This parameter is incompatible:
14: loadWrapped(obj); // errors
^ type application of polymorphic type: Descriptor. Has some incompatible type argument with
9: function loadBasic(obj: Descriptor): void {}
^ type application of polymorphic type: Descriptor
Type argument `T` is incompatible:
13: function load(obj: Descriptor) {
^ T. This type is incompatible with
13: function load(obj: Descriptor) {
^ some incompatible instantiation of `T`
```

It certainly seems like Flow should be able to see that the input type is the same for both.

It works if the wrapper is

```
function wrap(fn: Fn): Fn {
```
but this is a trimmed down example. In my case I'm explicitly using a function because I'm adding an additional parameter.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.