facebook / facebook/flow

Constructing a type with intersection operator causes error if that type is later used in a union

Đang mở
#5,000 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
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ả

If you use an intersection operator to make a type, and then use that in a union, you can no longer query any properties that may be absent on some of the union members. But if you do not use the intersection operator, and just add in the the property directly, then it works just fine.

```js
// @flow

type A1 = { a:string, data: string }
type B1 = { b:string, data: string }
type X1 = A1 | B1

type Common = { data: string }
type A2 = { a:string } & Common
type B2 = { b:string } & Common
type X2 = A2 | B2

// X1 and X2 should be functionally identical.

function foo1(entry: X1) {
if (entry.a) console.log('foo!') // Works!
}

// Error!
function foo2(entry: X2) {
if (entry.a) console.log('foo!') // Property cannot be accessed on any member of intersection type
}
```

[Try flow link](https://flow.org/try/#0PTAEAEDMBsHsHcBQiSgOqwE4GsDOBCRAFwE8AHAU1AEEBGUAXlAG9QBDALlyMwEsA7AOYAaUABM2RTqG58hoAL7FyVAEL0mrAEZceAkeMnTZ+xcsqgAGhpr0APqHXJUAFQAWvXLfb8xNAEwybrAArtB+bmwAblQUAB5sAMZE0CSgRG5UuGwAtlmRlKJaIUSgmBS4YUReAj6gFJiYWKDwmfygIbgUYgB05lQAwrA5ObDtmoZSHDJ68kqkFtSBE5wmc6AAZKBDI2P9jsssoDprgoqb28Oj-PuWh0ugDqr+zmAYOASIkCH8ybxjoEgsFgtAAFBR+DwSNNrABKFiIUCgXiQUDgyGYEg9NjwxJjXCwaAUHpwQSggDkQNg+HJsMQShQYAAoo0sIRvr8iP92lT-OioTD-PDmIjkaj+Zjsbj8YTiaSKVSaXSFEA)

It shouldn't matter how you construct a type that ends up in a union, right?

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.