facebook / facebook/flow

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

Aperta
#3,391 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

```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
}
```

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.