facebook / facebook/flow

[bug] Intersection with a tagged union unable to refine type

Offen
#3,391 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Rust
Sterne
22.3k
Forks
1.9k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

```js
/* @flow */

type A = {a: string}
type B = {b: string, type: 'b'}
type C = {c: string, type: 'c'}
type BorC = B | C
type ABC = A & BorC

const m: ABC = {type: 'b', a: '', b: ''}

if (m.type === 'b') {
m.b // Error!
// Also interesting. If you have this line, then the `const m` line gets
// a flow error. Remove the line, and the error on `const m` goes away.
}
```

[run it on tryflow](https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCCYAvGAN4CGAXGAM4YBOAlgHYDmAvprgQEKkKAI1oMWHADRhs+WgHIhc7jIIBhQeQDGopm3ZSV8zUp74wfOI3VkBAHzCrTBQn2tEwAMnOXHKogLJiLz4nIjcgh1CXS0JwuzD0TThWBjAAW1oXN3JDMAU5KRE8ygKwGjyTYFBUZigwAAo0gDo-Eja8xQBKClQwdKahVG4q5DRUJJSMMFZMi0ZYjVz8qXK5UqK17lqG1hbeUnb87vJe6YGhoA)

However, a union of intersections works fine
```js
type A = {a: string}
type B = {b: string, type: 'b'}
type C = {c: string, type: 'c'}

type AB = A & B
type AC = A & C
type ABorAC = AB | AC

const n: ABorAC = {type: 'b', a: '', b: ''}
if (n.type === 'b') {
n.b // no error
}
```

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.