facebook / facebook/flow

Intersection of object types with matching keys does not work as expected.

Open
#5,379 0 comments 3 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 sure if this is a bug.
```
/* @flow */
type Intersect = string & 'hello world';
const x: Intersect = 'hello world'; // no issue
type IntersectObjects = { kind: string } & { kind: 'hello world' };
const y: IntersectObjects = { kind: 'hello world' }; // suddenly an issue
```
produces the following error output:
```
5: const y: IntersectObjects = { kind: 'hello world' };
^ object literal. This type is incompatible with
4: type IntersectObjects = { kind: string } & { kind: 'hello world' };
^ object type
Property `kind` is incompatible:
4: type IntersectObjects = { kind: string } & { kind: 'hello world' };
^ string. Expected string literal `hello world`
4: type IntersectObjects = { kind: string } & { kind: 'hello world' };
^ string literal `hello world`
```.

I would expect this to work as per the documentation re: Intersection Types:
"But when you have properties that overlap by having the same name, it creates an intersection of the property type as well.

For example, if you merge two objects with a property named prop, one with a type of number and another with a type of boolean, the resulting object will have an intersection of number and boolean."

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.