facebook / facebook/flow

Object literal warning on union objects

Open
#4,683 1 comment 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

Here is a minimal test case for an issue I came across.

```javascript
// @flow
export type Apple = { type: 'apple' };
export type Oranges = { type: 'oranges' };

export type Fruit = Apple | Oranges;

function createFruit(type: 'apple' | 'oranges'): Fruit {
return { type };
}
```

Produces the following error:

```
8: return { type };
^ object literal. This type is incompatible with
7: function createFruit(type: 'apple' | 'oranges'): Fruit {
^ union: Apple | Oranges
```

Live example: [flow.org/try](https://flow.org/try/#0PTAEAEDMBsHsHcBQBTAHgB1gJwC6hwJ7rKgCC660JAvKAN75HIBcoA5AIYVVugC+AbhQZseQsVAB5LBwB2Ac2QBnULQbiW7bHMVLegxMMy5GEgGJYArgEs8tcpRIAfKTIXKhiSJdkBjHNawsqC+WMgcOMgWNjgAFBqsnNzIvC5s2u56AJSs0bb0iKCgYTiWWMHqTPxCfIhAA)

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.