Intersection of object types with matching keys does not work as expected.
- 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
Assessment
This issue has not been assessed yet.