facebook / facebook/flow

Intersection with disjoint unions generates incorrect invalid access errors

Đang mở
#3,136 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
needs triage Typing: unions/intersections
Ngôn ngữ chính
Rust
Star
22.3k
Fork
1.9k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Flow: 0.37 (back until as long as I remember 0.2x)

Flow gives incorrect "Property cannot be accessed on any member of intersection type" in many situations where it doesn't make sense.

Consider access to the variable `errorMessage` in the following code:

```
var result : Result = Math.random(2.0) ? {
identifier: 1,
error: true
} : {
identifier: 2,
error: false,
object: {}
}

if (result.error) {
console.log(result.errorMessage)
}
```

in conjunction with the following type definitions for `Result`.

#### Definition 1 (Error)

```
type Result = {identifier: number} & ({
error: true,
errorMessage?: string
} | {
error: false,
object: Object
})
```

which results in a property cannot be accessed error for `errorMessage`.

However, this equivalent definition for `Result` functions correctly with no errors:

#### Definition 2 (Pass)

```
type Result = ({identifier: number} & {
error: true,
errorMessage?: string
}) | ({identifier: number} & {
error: false,
object: Object
})
```

Now removing the intersection altogether gets rid of the error as expected (a simple disjoint union):

#### Definition 3 (Pass)

```
type Result = {
error: true,
errorMessage?: string
} | {
error: false,
object: Object
}
```

However, to demonstrate the problem in its simplest form, including an intersection with even a empty object type throws a spanner in the works:

#### Definition 4 (Error)

```
type Result = {} & ({
error: true,
errorMessage?: string
} | {
error: false,
object: Object
})
```

Basically flow cannot handle intersections of disjoint unions, however it can handle disjoint unions of intersections (definition 2).

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.