facebook / facebook/flow

Complex union type should work, but doesn't

Open
#6,291 3 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

I'm trying to express an admittedly fairly complex type, where it has to have at least one of two properties, if it has a certain property, it must have another property, etc. However, I'm getting weird errors.

Here's the type
```js
export type learningItemT = {
name: string,
id: string
} & (
| { dataStructure: any, Editor: React.ComponentType }
| { Creator: React.ComponentType, Editor?: React.ComponentType }
) &
(
| {
ThumbViewer: React.ComponentType,
Viewer?: React.ComponentType
}
| {
ThumbViewer?: React.ComponentType,
Viewer: React.ComponentType
}
);
```

When I declare an element, I get no errors.

```js
const f = () =>

Hi


const f1 = () =>

Hi


const f2 = () =>

Hi

const a = ({
name: 'file',
id: 'li-file',
Viewer: f,
ThumbViewer: f1,
Creator: f2
}: learningItemT);
```

However, when I try to access Creator, which is a valid member of the type above (otherwise the assignment should already have failed?), it doesn't let me:

```js
if(a.Creator) { console.log(a.Creator) }
```

(Flow error)

If I isolate this single union type, and do exactly the same, it works perfectly (no errors):

```js
type c = (
| { dataStructure: any, Editor: React.ComponentType }
| { Creator: React.ComponentType, Editor?: React.ComponentType }
)

const newtry: c = {Creator: f1}
if(newtry.Creator) { console.log('this time it worked') }
```

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBLAtgBzgJwBdkwBDAZzACUBTUgY2KnzizAHJ87H2BudGgA88RMIQCeOGmBh18AOwzyA5gElCNLABUwAXjABvVGDDzSWGgC4w5QviXKANMbAYAJtdv2VqAL5gAMjAAChcAH0MwN1JCUgBlOwBXRkSua1J5cUcwAFE3DEICa1oGQgA6AGFWPHkaeUItSRoAHgzxAD4wX3DIiq4YoupucqrcOFr6xqlWzPbsvIKCAH5i4crq8bqGppmOrtQASkCXUJMTCKMzq7AtAAtErAAjADUMGgQafFXS9bGJ7embTmLmuYFe70+KyGP1GNS2UxaQJBZ26VwuyKudweLzeH3wUJKjF+cMmOyBzlBJnBeO+RNhm1JgNmGP2JgO-FQ9HGtjAUD0ISOuk6zVuAEZ2gAJDDNYBi9qc7lMUX84KC4VyqUyuUK+Q8qAAJhVarAIvFmtl4vQXN1xFI-JCl1M5isHCgGFk7Aprg8HBgGAAtG6PV7qZ9rFAvVinqGvrzRV6+nRCrGDX5rLJSAoHOpNFp2egMFBgqRKv1k0cDGBreQ4LIyvBlMXS0mCEdusYJFIqyqepXorEEvhkoRUi62vN8snaSMNv8EbtOqiwBcwImBrHCTO-vCybMJ4t8dPiQyAYjZvsDlbFaZ3nZxNZ6PyDGup3HuoXgrUEHfm+uK1XuVrGh6zgRt2EIW4MEoQhsGkAowAQAgAGsaDcdg2yAA

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.