flow-runtime icon indicating copy to clipboard operation
flow-runtime copied to clipboard

Function intersections/unions in $ObjMap don't work

Open k15a opened this issue 7 years ago • 3 comments

This is a:

  • Bug Report

Which concerns:

  • flow-runtime

What is the current behaviour?

The following code breaks at runtime:

type $DeepShape<O: Object> = Object &
  $Shape<$ObjMap<O, (<V: Object>(V) => $DeepShape<V>) & (<V>(V) => V)>>;

type Thing = {
  a: string,
  b: string,
  c: {
	  a: string,
  }
}

type Hello = $DeepShape<Thing>
  
const hello: Hello = {
	b: 'hello',
}

console.log(hello)

The $DeepShape type is from https://github.com/facebook/flow/issues/5262.


What is the expected behaviour?

The code should not break at runtime and should run without type errors.


Which package versions are you using?

0.17.0

k15a avatar Feb 08 '18 13:02 k15a

This turns out to be due to this part - (<V: Object>(V) => $DeepShape<V>) & (<V>(V) => V) - which is an intersection of two functions. I was surprised to see that Flow supports this because it doesn't appear to be possible to create a real value that satisfies this intersection.

phpnode avatar Feb 08 '18 14:02 phpnode

The other option there would be to use a union type instead of an intersection. Then I get the following error:

Mapper must be a function type.

k15a avatar Feb 08 '18 14:02 k15a

@phpnode

I was surprised to see that Flow supports this because it doesn't appear to be possible to create a real value that satisfies this intersection.

I don't get what you mean, I think any function that deletes some properties of the given object tree when called with an object, or returns a primitive of the same type otherwise, would satisfy that intersection

jedwards1211 avatar Feb 08 '18 15:02 jedwards1211